Forum: Ruby on Rails Variable Method Name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
62ca565e0da4a0014621c8d1d9ba44fd?d=identicon&s=25 BlackNute (Guest)
on 2007-01-17 20:53
(Received via mailing list)
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
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-01-17 22:09
(Received via mailing list)
>
> 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...
B8dbb1987e8e5318584865f880036796?d=identicon&s=25 Chris Wanstrath (defunkt)
on 2007-01-17 22:15
(Received via mailing list)
On 1/12/07, BlackNute <blacknute@gmail.com> wrote:

> variable_name = 'text'
> print @#{variable_name}

instance_variable_get("@#{variable_name")

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


--
Chris Wanstrath
http://errtheblog.com
B8dbb1987e8e5318584865f880036796?d=identicon&s=25 Chris Wanstrath (defunkt)
on 2007-01-17 23:56
(Received via mailing list)
On 1/17/07, Chris Wanstrath <chris@ozmm.org> wrote:
> On 1/12/07, BlackNute <blacknute@gmail.com> wrote:
>
> > variable_name = 'text'
> > print @#{variable_name}
>
> instance_variable_get("@#{variable_name")

Whoops, should be

  instance_variable_get("@#{variable_name}")

of course.
4375b4aca04b8d72482fd409fb6d0b47?d=identicon&s=25 Roderick van Domburg (Guest)
on 2007-01-18 10:03
(Received via mailing list)
> variable_name = 'text'
> print @#{variable_name}

eval("print #{variable_name}")

--
Nedforce Informatica Specialisten B.V.
http://www.nedforce.nl
+31 (0)53 4500225
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-01-18 15:45
BlackNute wrote:
> 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 had asked questions like this.
http://www.ruby-forum.com/topic/93236#187853
http://www.ruby-forum.com/topic/93682#189535
This topic is locked and can not be replied to.