动态设置ruby变量


https://rosettacode.org/wiki/Dynamic_variable_names#Ruby

Ruby


p "Enter a variable name"
x = "@" + gets.chomp!
instance_variable_set x, 42
p "The value of #{x} is #{instance_variable_get x}"
 
Example output:

"Enter a variable name"
hello
"The value of @hello is 42"
阅读量: 836
发布于:
修改于: