# Contents of variable as variable name

**URL:** <https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458>\
**Category:** Ruby\
**Created:** [July 15, 2007, 5:10am UTC](https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458 "2007-07-15T05:10:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![redct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@redct](https://www.ruby-forum.com/u/redct)\
**Post date:** [July 15, 2007, 5:10am UTC](https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458/1 "2007-07-15T05:10:49Z")

</div>

I know that sounds a bit crazy, but is there a way to do that?

---

<div class="post-metadata">

**Author:** ![redct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@redct](https://www.ruby-forum.com/u/redct)\
**Post date:** [July 15, 2007, 5:23am UTC](https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458/2 "2007-07-15T05:23:55Z")

</div>

On 7/15/07, Rob K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![redct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@redct](https://www.ruby-forum.com/u/redct)\
**Post date:** [July 15, 2007, 6:33am UTC](https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458/3 "2007-07-15T06:33:59Z")

</div>

Rob K. 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:

> **[Register4Less.com Parking Page for jrandomhacker.info](http://www.jrandomhacker.info/Ruby_code_snippets/patching_strings_together_to_make_a_variable)**
>
> Parking page for domain jrandomhacker.info registered with Register4Less.com.

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

@ab = “pass”  
puts instance\_variable\_get(“@#{“a” + “b”}”)

---

<div class="post-metadata">

**Author:** ![redct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@redct](https://www.ruby-forum.com/u/redct)\
**Post date:** [July 16, 2007, 12:26am UTC](https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458/4 "2007-07-16T00:26:27Z")

</div>

On 7/15/07, Gregory S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sun, Jul 15, 2007 at 12:10:52PM +0900, Rob K. 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:
> 
> [Why it's stupid to `use a variable as a variable name'](http://perl.plover.com/varvarname.html)  
> [Why it's stupid to `use a variable as a variable name', Part II](http://perl.plover.com/varvarname2.html)  
> [Why it's stupid to `use a variable as a variable name', Part III](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.

---

<div class="post-metadata">

**Author:** ![redct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/97f17d/32.png) [@redct](https://www.ruby-forum.com/u/redct)\
**Post date:** [July 15, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/contents-of-variable-as-variable-name/109458/5 "2007-07-15T14:47:40Z")

</div>

On Sun, Jul 15, 2007 at 12:10:52PM +0900, Rob K. 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/varvarname.html)  
[http://perl.plover.com/varvarname2.html](http://perl.plover.com/varvarname2.html)  
[http://perl.plover.com/varvarname3.html](http://perl.plover.com/varvarname3.html)

–Greg
