Forum: Ruby Contents of variable as variable 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.
04235977db3e3779fb7456f8dfb493fa?d=identicon&s=25 Rob Kerfia (redct)
on 2007-07-15 05:10
I know that sounds a bit crazy, but is there a way to do that?
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 Hemant Kumar (gnufied)
on 2007-07-15 05:23
(Received via mailing list)
On 7/15/07, Rob Kerfia <redct@openfish.info> wrote:
> I know that sounds a bit crazy, but is there a way to do that?
>

One way of doing it is:

irb(main):025:0> a = "foo"
"foo"
irb(main):026:0> eval "#{a}=1"
1
irb(main):027:0> foo
1

Although eval is normally discouraged and must be used sparingly.
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 sy1234 (Guest)
on 2007-07-15 06:33
(Received via mailing list)
Rob Kerfia said the following on 07/14/2007 11:10 PM:
> I know that sounds a bit crazy, but is there a way to do that?
>
>
oh oh oh .. I asked this question and did a bit of experimentation a
while back.

I found a blank spot in my wiki where I was supposed to have written it
all down, but I guess I didn't get around to it.  I just found and
dumped some notes in just now.. maybe something there will help inspire
you:

http://jrandomhacker.info/Ruby_code_snippets/patch...

if eval isn't appropriate, then this snippet might work:

@ab = "pass"
puts instance_variable_get("@#{"a" + "b"}")
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2007-07-15 14:47
(Received via mailing list)
On Sun, Jul 15, 2007 at 12:10:52PM +0900, Rob Kerfia wrote:
> I know that sounds a bit crazy, but is there a way to do that?

Don't do it. If you think you need to, it is indicative of bad design.
There are significant dangers involved. The following is about Perl
rather
than Ruby, but the concept is the same:

http://perl.plover.com/varvarname.html
http://perl.plover.com/varvarname2.html
http://perl.plover.com/varvarname3.html

--Greg
316017672dfe1c4e8e22843348ee32d4?d=identicon&s=25 Sy Ali (Guest)
on 2007-07-16 00:26
(Received via mailing list)
On 7/15/07, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:
> On Sun, Jul 15, 2007 at 12:10:52PM +0900, Rob Kerfia wrote:
> > I know that sounds a bit crazy, but is there a way to do that?
>
> Don't do it. If you think you need to, it is indicative of bad design.
> There are significant dangers involved. The following is about Perl rather
> than Ruby, but the concept is the same:
>
> http://perl.plover.com/varvarname.html
> http://perl.plover.com/varvarname2.html
> http://perl.plover.com/varvarname3.html

Thanks for the reading.  This helped me avoid learning a Bad habit.

This comment from the second document summed it up for me:
  The real problem is that if your string contains something
unexpected, it will sabotage a totally unrelated part of the program,
and then you will have one hell of a time figuring out the bug.

Now the next time I itch to do something like this I'll explore using
a hash instead.
This topic is locked and can not be replied to.