Reputation: 12207
So I have a for loop thats creating a hash or array depending on whats being passed in.
I need to create these arrays and Hashes with names based on whats being passed in.
Its much the same as
window['MyNewArray-' + i] = [];
In javascript. Is there any equivalent for Ruby?
Upvotes: 2
Views: 2672
Reputation: 1717
Well you can create a Ruby hash using :
h = {}
and then add a key/value pair using the store
or the []=
operator.
Like this :
h["foo_#{i}"] = []
Upvotes: 2
Reputation: 369536
That same code does work in Ruby, too, and does the same thing.
Upvotes: 2
Reputation: 20724
You could do something like:
window = {}
5.times do |i|
window["my_new_array_#{i}"]=[]
end
Upvotes: 4