Initializing a multidimensional array


#1

i have a plain old array initialized like this:

@columns = Array.new

this array needs to be an array of arrays…

how do i tell ruby that this is an array of arrays?

what i want to do is something like this:

@columns[i] << ‘some value’

where is is determined programattically…

obviously, ruby won’t let me do this since @columns[i] is not an
array, but an array element…

any help would be GREATLY appreciated!


#2

sergio_101 wrote:

@columns[i] << ‘some value’

where is is determined programattically…

obviously, ruby won’t let me do this since @columns[i] is not an
array, but an array element…

Actually ruby is just fine with that. You can put any object in an
array, including other arrays. No
need to “tell” ruby anything special. Here’s an example:

irb(main):001:0> a = []
=> []
irb(main):003:0> b = [1,2,3]
=> [1, 2, 3]
irb(main):004:0> c = [4,5,6]
=> [4, 5, 6]
irb(main):005:0> a << b
=> [[1, 2, 3]]
irb(main):006:0> a << c
=> [[1, 2, 3], [4, 5, 6]]
irb(main):007:0> a[0] << 7
=> [1, 2, 3, 7]
irb(main):008:0> a
=> [[1, 2, 3, 7], [4, 5, 6]]

As long as there is an object at the index you specify and that object
responds to ‘<<’, this code
will succeed.

b