# Multidimensional array insert syntax

**URL:** <https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311>\
**Category:** Ruby\
**Created:** [July 28, 2010, 4:02am UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311 "2010-07-28T04:02:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 4:02am UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/1 "2010-07-28T04:02:36Z")

</div>

Hi,

Can anyone help me with the correct syntax to insert into a  
multidimensional array…

answer\_choices = [  
[incorrect\_ans\_2, incorrect\_anno\_2],  
[incorrect\_ans\_3, incorrect\_anno\_3],  
[incorrect\_ans\_4, incorrect\_anno\_4],  
[incorrect\_ans\_5, incorrect\_anno\_5]  
]

# Randomly insert correct answer.

x = rand(4)  
answer\_choices.insert??? correct\_ans\_1  
answer\_choices.insert??? correct\_anno\_1

I want to insert correct\_ans\_1 into position [x,0] and correct\_anno\_1  
into position [x,0].

Thanks,

DC

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 11:03am UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/2 "2010-07-28T11:03:56Z")

</div>

On Wed, Jul 28, 2010 at 4:02 AM, Dave C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ]
> 
> # Randomly insert correct answer.
> 
> x = rand(4)  
> answer\_choices.insert??? correct\_ans\_1  
> answer\_choices.insert??? correct\_anno\_1
> 
> I want to insert correct\_ans\_1 into position [x,0] and correct\_anno\_1  
> into position [x,0].

You nearly had it in that sentence:

answer\_choices[x][0] = correct\_ans\_1  
answer\_choices[x][1] = correct\_ano\_1

(I assume you wanted to insert the correct\_anno\_1 in position 1 of the  
array).

Jesus.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 1:09pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/3 "2010-07-28T13:09:49Z")

</div>

On Wed, Jul 28, 2010 at 12:56 PM, Dave C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > array).
> > 
> > Jesus.
> 
> I actually want to insert rather than replace. Is there a way to do  
> that??

[http://ruby-doc.org/core/classes/Array.src/M002161.html](http://ruby-doc.org/core/classes/Array.src/M002161.html)

irb(main):001:0\> a = [1,2,3,4]  
=\> [1, 2, 3, 4]  
irb(main):002:0\> a[2,0] = 55  
=\> 55  
irb(main):003:0\> a  
=\> [1, 2, 55, 3, 4]  
irb(main):004:0\> a[2,0] = \*[100,200,300]  
=\> [100, 200, 300]  
irb(main):005:0\> a  
=\> [1, 2, 100, 200, 300, 55, 3, 4]

and for multidimensional:

irb(main):006:0\> a = [[1,2,3],[4,5,6]]  
=\> [[1, 2, 3], [4, 5, 6]]  
irb(main):007:0\> a[0][1,0] = 55  
=\> 55  
irb(main):008:0\> a  
=\> [[1, 55, 2, 3], [4, 5, 6]]

Take into account that in Ruby arrays are not really  
“multidimensional”. They are just arrays that contain other arrays.  
It’s not ensures that each array of a “dimension” has the same length,  
for example. When you do a[0] in the above, you get an array, on which  
you call the []= method.

Jesus.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 12:56pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/4 "2010-07-28T12:56:29Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Wed, Jul 28, 2010 at 4:02 AM, Dave C.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ]
> > 
> > # Randomly insert correct answer.
> > 
> > ï¿½x = rand(4)  
> > ï¿½answer\_choices.insert??? ï¿½correct\_ans\_1  
> > ï¿½answer\_choices.insert??? ï¿½correct\_anno\_1
> > 
> > I want to insert correct\_ans\_1 into position [x,0] and correct\_anno\_1  
> > into position [x,0].
> 
> You nearly had it in that sentence:
> 
> answer\_choices[x][0] = correct\_ans\_1  
> answer\_choices[x][1] = correct\_ano\_1
> 
> (I assume you wanted to insert the correct\_anno\_1 in position 1 of the  
> array).
> 
> Jesus.

I actually want to insert rather than replace. Is there a way to do  
that??

Thanks!

DC

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/5 "2010-07-28T13:39:06Z")

</div>

On Wed, Jul 28, 2010 at 1:10 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use Array#insert.

Amazing how I missed that, given that I was specifically looking for  
it in the rdoc 😃  
I call “sleep deprived”, or something

Jesus.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 2:26pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/6 "2010-07-28T14:26:34Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Wed, Jul 28, 2010 at 1:10 PM, Robert K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Use Array#insert.
> 
> Amazing how I missed that, given that I was specifically looking for  
> it in the rdoc 😃  
> I call “sleep deprived”, or something
> 
> Jesus.

That works for single dimension arrays but I can’t figure out the syntax  
for multi…

I have been trying to use “answer\_choices.insert” but cannot figure out  
the syntax  
eg answer\_choices.insert([x][1],correct\_ans\_1) does not work…

Could you give me an example of Array#insert. using multidim. array

Thanks,

DC

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/7 "2010-07-28T15:19:49Z")

</div>

On Wed, Jul 28, 2010 at 2:26 PM, Dave C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Jesus.
> 
> That works for single dimension arrays but I can’t figure out the syntax  
> for multi…
> 
> I have been trying to use “answer\_choices.insert” but cannot figure out  
> the syntax  
> eg answer\_choices.insert([x][1],correct\_ans\_1) does not work…
> 
> Could you give me an example of Array#insert. using multidim. array

In order to better understand it, I’m afraid you will need to stop  
thinking about multidimensional arrays, and start thinking of arrays  
of arrays. First you need to get the inner array:

answer\_choices[x]

then you get back an array. It’s on this inner array where you want to  
insert, so (split in two steps):

inner = answer\_choices[x]  
inner.insert(0, correct\_ans\_1)

Jesus.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/8 "2010-07-28T15:22:12Z")

</div>

Dave C. wrote:

> JesÃºs Gabriel y GalÃ¡n wrote:
> 
> > On Wed, Jul 28, 2010 at 1:10 PM, Robert K.  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Use Array#insert.
> > 
> > Amazing how I missed that, given that I was specifically looking for  
> > it in the rdoc 😃  
> > I call “sleep deprived”, or something
> > 
> > Jesus.
> 
> That works for single dimension arrays but I can’t figure out the syntax  
> for multi…
> 
> I have been trying to use “answer\_choices.insert” but cannot figure out  
> the syntax  
> eg answer\_choices.insert([x][1],correct\_ans\_1) does not work…
> 
> Could you give me an example of Array#insert. using multidim. array
> 
> Thanks,
> 
> DC

Sorry, missed your reply earlier. That was what I needed. Thank you  
for the help!

DC

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [July 28, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/multidimensional-array-insert-syntax/192311/9 "2010-07-28T13:11:35Z")

</div>

On 28.07.2010 12:56, Dave C. wrote:

> > > 
> > 
> > array).
> > 
> > Jesus.
> 
> I actually want to insert rather than replace. Is there a way to do  
> that??

Use Array#insert.

robert
