variable variables

I know this can be done.. rails creates methods from variables all the time... but how do you code it?

say you have two strings.

a="test" test="this is a test"

how do you get the contents of test with the variable a?

#{a}

well that doesn;t work!! I know this is a easy question for the ruby gods out there.. but for a newbie learning the language it's hard.. :> HELP!!!

say you have two strings.

a="test" test="this is a test"

how do you get the contents of test with the variable a?

Go read "The Ruby Way"

class A attr_accessor :variable, :test end

a = A.new

a.variable = :test a.send a.variable if a.responds_to? a.variable

send is a method in class Object that takes a symbol and an argument list. the symbol is used to call a method of a corresponding name which is given the arguments.

Rails tends to use this in combination with method missing in active record for its handling of database attributes which I assume is what your referring to.

-K

a = "test" test = "this is a test" eval a # => "this is a test"

Or depending on what you're doing, why not do this:

a = "This is my #{test} variable." test = "super cool"

-Ryan Kopf