Evaluating a variable from it's name

If I have a string variable that contains the "name" of another variable. How can I access that other variable for both assignment and evaluation?

e.g.

x = 100 nameofvar = "x"

I want to set the value of the variable specified by nameofvar and also get it's value.

Seems like some sort of reflection calls would be needed for this...

Hi --

Hello,

See the following code:

class Reflection    attr_accessor :x    def y      "y"    end    def call_method (method_name)       self.send(method_name)    end end

Now you can do the following things:

r = Reflection.new

=> #<Reflection:0xb7172b60>

r.call_method("y")

=> "y"

r.call_method("x")

=> nil

r.x = "test"

=> "test"

r.call_method("x")

=> "test"

Best regards, Jeroen