# Initializing a multidimensional array

**URL:** <https://www.ruby-forum.com/t/initializing-a-multidimensional-array/104488>\
**Category:** Rails\
**Created:** [May 18, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/initializing-a-multidimensional-array/104488 "2007-05-18T17:28:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [May 18, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/initializing-a-multidimensional-array/104488/1 "2007-05-18T17:28:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [May 20, 2007, 2:42am UTC](https://www.ruby-forum.com/t/initializing-a-multidimensional-array/104488/2 "2007-05-20T02:42:42Z")

</div>

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
