# Newby question determining name of an array at run-time

**URL:** <https://www.ruby-forum.com/t/newby-question-determining-name-of-an-array-at-run-time/77854>\
**Category:** Ruby\
**Created:** [December 1, 2006, 12:49am UTC](https://www.ruby-forum.com/t/newby-question-determining-name-of-an-array-at-run-time/77854 "2006-12-01T00:49:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfonso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbe5ce/32.png) [@Alfonso](https://www.ruby-forum.com/u/Alfonso)\
**Post date:** [December 1, 2006, 12:49am UTC](https://www.ruby-forum.com/t/newby-question-determining-name-of-an-array-at-run-time/77854/1 "2006-12-01T00:49:57Z")

</div>

(Sorry for not finding a better title to my question. I’m not shure if  
this has something to do with metaprogramming… I have hear about that,  
but never had time to learn about it, so I don’t know what is  
metaprogramming exactly)

Is there a way to do something like this?

arr = [“red”, “green”, “yellow”]

# the following arrays should be incremented with the loop down

@red = []  
@green = []  
@yellow = []

#here the loop that should increment the arrays  
20.times do  
arr.each { |color|  
rand(60)  
unless @color.include?(aleat) #this doesn’t work  
@color.push(aleat) # this doesn’t work…  
end  
}  
end  
…  
…

Thanks in advance.

* * *

LLama Gratis a cualquier PC del Mundo.  
Llamadas a fijos y móviles desde 1 céntimo por minuto.  
[http://es.voice.yahoo.com](http://es.voice.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Alfonso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbe5ce/32.png) [@Alfonso](https://www.ruby-forum.com/u/Alfonso)\
**Post date:** [December 1, 2006, 3:56am UTC](https://www.ruby-forum.com/t/newby-question-determining-name-of-an-array-at-run-time/77854/2 "2006-12-01T03:56:32Z")

</div>

On 12/1/06, Alfonso [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # the following arrays should be incremented with the loop down
> 
> ```
> end
> 
> ```
> 
> }  
> end  
> …  
> …

Have a look at instance\_variable\_get in the RDocs - that’ll do what you  
want.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![Alfonso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbe5ce/32.png) [@Alfonso](https://www.ruby-forum.com/u/Alfonso)\
**Post date:** [December 1, 2006, 1:36am UTC](https://www.ruby-forum.com/t/newby-question-determining-name-of-an-array-at-run-time/77854/3 "2006-12-01T01:36:07Z")

</div>

Alfonso wrote:

> # the following arrays should be incremented with the loop down
> 
> @red = []  
> @green = []  
> @yellow = []

Are you after named arrays? Do it this way:

hash = {}

hash[:red] = []  
hash[:green] = []  
hash[:blue] = []

hash.keys.each do |color|  
array = hash[color]  
#do something here  
end

But if you need to go through the colors in a particular order:

[:red,:green,:blue].each do |color|

# etc.

> #here the loop that should increment the arrays  
> 20.times do  
> arr.each { |color|  
> rand(60)  
> unless @color.include?(aleat) #this doesn’t work  
> @color.push(aleat) # this doesn’t work…  
> end  
> }  
> end

It always helps for you to say what you are trying to do, not just that  
it  
didn’t work.

---

<div class="post-metadata">

**Author:** ![Alfonso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbe5ce/32.png) [@Alfonso](https://www.ruby-forum.com/u/Alfonso)\
**Post date:** [December 1, 2006, 1:07pm UTC](https://www.ruby-forum.com/t/newby-question-determining-name-of-an-array-at-run-time/77854/4 "2006-12-01T13:07:40Z")

</div>

The method instance\_variable\_get was exactly what I needed.

Thank you very much for your answers

* * *

LLama Gratis a cualquier PC del Mundo.  
Llamadas a fijos y móviles desde 1 céntimo por minuto.  
[http://es.voice.yahoo.com](http://es.voice.yahoo.com)
