newby question determining name of an array at run-time

A

Alfonso

(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
 
M

Max Muermann

(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
...
...

Have a look at instance_variable_get in the RDocs - that'll do what you want.

Cheers,
Max
 
A

Alfonso

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top