I'd like to know if there is something Ruby that does something like this:
@my_var = "foo"
some_function_i_dont_know_name_of("@my_var")
=> "foo"
Seems like you are looking for instance_variable_get. From the docs:
Returns the value of the given instance variable, or
nilif the instance variable is not set. The@part of the variable name should be included for regular instance variables. Throws aNameErrorexception if the supplied symbol is not valid as an instance variable name. String arguments are converted to symbols.class Fred def initialize(p1, p2) @a, @b = p1, p2 end end fred = Fred.new('cat', 99) fred.instance_variable_get(:@a) #=> "cat" fred.instance_variable_get("@b") #=> 99
eval("@my_var")would also do.evalis evil.