eval with variable name : eval « Development « Ruby






eval with variable name


x = 1
varname = "x"
eval(varname)           # => 1
eval("varname = '$g'")  # Set varname to "$g"
eval("#{varname} = x")  # Set $g to 1
eval(varname)           # => 1

 








Related examples in the same category

1.Use eval to run a statement dynamically
2.Execute an eval in a thread
3.Evaluation with variable
4.Add a new method with eval