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.
BlackNute (Guest)
on 2007-01-17 21: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
Philip H. (Guest)
on 2007-01-17 23: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...
Chris W. (Guest)
on 2007-01-17 23:15
(Received via mailing list)
On 1/12/07, BlackNute <removed_email_address@domain.invalid> wrote:

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

instance_variable_get("@#{variable_name")

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


--
Chris W.
http://errtheblog.com
Chris W. (Guest)
on 2007-01-18 00:56
(Received via mailing list)
On 1/17/07, Chris W. <removed_email_address@domain.invalid> wrote:
> On 1/12/07, BlackNute <removed_email_address@domain.invalid> wrote:
>
> > variable_name = 'text'
> > print @#{variable_name}
>
> instance_variable_get("@#{variable_name")

Whoops, should be

  instance_variable_get("@#{variable_name}")

of course.
Roderick van Domburg (Guest)
on 2007-01-18 11: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
Nanyang Z. (Guest)
on 2007-01-18 16: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.