ruby - Get value from string representing local variable -
this question has answer here:
i have local variable name string, , need value.
variable = 22 "variable".to_variable?
how can value 22
form string?
you can use eval
.
variable = 22 eval("variable") # => 22
however eval
can nasty. if dont mind declaring instance variable, can too:
@variable = 22 str = "variable" instance_variable_get("@#{str}") # => 22
Comments
Post a Comment