Variable Method Name

Hello List!

I come to you after a long while of troubleshooting and researching to no avail. My wish is to reference a class variable whose name is stored as a string in another variable. I will try to set up an example:

variable_name = 'text' print @#{variable_name}

I think that is how it would work, and i think the syntax is nearly correct, but... its not.

Any help? Thanks alot guys!

--nathan

I come to you after a long while of troubleshooting and researching to no avail. My wish is to reference a class variable whose name is stored as a string in another variable. I will try to set up an example:

variable_name = 'text' print @#{variable_name}

I think that is how it would work, and i think the syntax is nearly correct, but... its not.

Any help? Thanks alot guys!

Not quite the same, but let's say you have an AR model named Foo that has an attribute named bar, then:

myfoo = Foo.find(:first) myfoo.send('bar') # same as myfoo.bar

Maybe that will get you closer...

instance_variable_get("@#{variable_name")

http://ruby-doc.org/core/

Whoops, should be

  instance_variable_get("@#{variable_name}")

of course.

variable_name = 'text' print @#{variable_name}

eval("print #{variable_name}")