I have a few similarly named arrays and I need to loop through the array names:
level_1_foo
level_2_foo
level_3_foo
level_4_foo
level_5_foo
I'd like to do something like the following where x is the value of the digit in the array name:
(1..5).each do |x|
level_x_foo << some-value
end
Can this be done?
I've tried "level_#{x}_foo", level_+x+_foo, and a couple others, to no success.
Thanks
data = {1 => some_value, 2 => some_other_value}You can then easily access the data withdata[1]. Having to "dynamically" access a variable is generally an anti-pattern and should be replaced with actual data structures (which is also much faster usually).