Forum: Ruby on Rails initializing a multidimensional array..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0a26b2a889f8f9273263e768311375af?d=identicon&s=25 Sergio Ruiz (sergio-101)
on 2007-05-18 17:28
(Received via mailing list)
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!
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-05-20 02:42
(Received via mailing list)
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
This topic is locked and can not be replied to.