# Referencing Variables By Name in a String

**URL:** <https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118>\
**Category:** Ruby\
**Created:** [March 5, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118 "2009-03-05T22:27:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 5, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118/1 "2009-03-05T22:27:47Z")

</div>

I can think of several (rather ugly) ways to accomplish this, but  
thought I’d check here to see if there is a simpler way to do this. I’m  
constantly amazed by the features in Ruby (I admit to only knowing a  
relatively small subset of them)

Assume I have objects (created in autogenerated code that frequently  
gets regenerated. I can’t change this part of the code directly  
(QTRuby), so creating them in an array isn’t really an option.

The names of the variables are pauseBox1, pauseBox2, pauseBox3 …  
pauseBox8.  
There are several variable groups like this - this is only one of them.  
I periodically have to apply a block of code to each of them in  
sequence. They’re not in an array, so I can’t directly enumerate over  
them. It would be kinda nice to be able to do something like:

1.upto(8) do |i|  
box = “pauseBox#{i}”  
  
  
end

Does anyone know a way to accomplish something like this. If not, I  
probably have to put code in the derived class to create the arrays, but  
this is going to look messy to say the least … there are a lot of  
these items.

Thanks in advance  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 6, 2009, 12:27am UTC](https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118/2 "2009-03-06T00:27:15Z")

</div>

Is this what you’re looking for?

1.upto(8) do |i|  
box = eval(“pauseBox” + i.to\_s)  
#do stuff with box  
end

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 6, 2009, 12:38am UTC](https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118/3 "2009-03-06T00:38:17Z")

</div>

Sharagoz – wrote:

> Is this what you’re looking for?
> 
> 1.upto(8) do |i|  
> box = eval(“pauseBox” + i.to\_s)  
> #do stuff with box  
> end

Exactly. Thanks … I forgot about eval.

Appreciate it much.  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 8, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118/4 "2009-03-08T20:30:52Z")

</div>

Michael S. wrote:

> Sharagoz – wrote:
> 
> > Is this what you’re looking for?
> > 
> > 1.upto(8) do |i|  
> > box = eval(“pauseBox” + i.to\_s)  
> > #do stuff with box  
> > end
> 
> Exactly. Thanks … I forgot about eval.
> 
> Appreciate it much.  
> —Michael

Would it be possible to do the same thing, but generating the variable  
name?  
i.e.  
1.upto(8) do |i|  
“pauseBox” + i.to\_s = i + 1  
#do stuff with box  
end

This don’t work

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 8, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/referencing-variables-by-name-in-a-string/162118/5 "2009-03-08T20:37:57Z")

</div>

Rg Rg schrieb:

> > Appreciate it much.  
> > This don’t work  
> > hm…if you have several variables with names like pauseBox1,  
> > pauseBox2, you did something wrong. For this Array exists.  
> > If this is not an Option there are several other ways (const\_get,  
> > instance\_variable\_get, …)
