# \[\]= with moredimensional Arrays

**URL:** <https://www.ruby-forum.com/t/with-moredimensional-arrays/73194>\
**Category:** Ruby\
**Created:** [October 22, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/with-moredimensional-arrays/73194 "2006-10-22T18:05:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [October 22, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/with-moredimensional-arrays/73194/1 "2006-10-22T18:05:50Z")

</div>

Hi all,

I want to code the “game of life” in ruby. I use a moredimensional  
array as the field and another one to provide an easy way to input the  
pattern of the start-population. (See the code below)  
So my question is, whether there is a possibility to use ar[a…b][x…y]  
= [[m, …], [n, …], …]. That means, how can I merge two  
2-dimensional arrays?

size = ARGV.first.to\_i  
field = Array.new(size, Array.new(size, 0))

thing = [[1, 1, 0, 1],  
[1, 0, 1, 0],  
[0, 0, 1, 0],  
[1, 1, 0, 0]]

pos\_x, pos\_y = size/2, size/2

field[pos\_x…(pos\_x + thing.length)][pos\_y…(pos\_y + thing.length)] =  
thing

Thanks,  
naPOLeon

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [October 22, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/with-moredimensional-arrays/73194/2 "2006-10-22T18:15:51Z")

</div>

naPOLeon wrote:

> Hi all,
> 
> I want to code the “game of life” in ruby. I use a moredimensional  
> array

Define “Moredimensional”. Do you mean multidimensional?

> as the field and another one to provide an easy way to input the  
> pattern of the start-population. (See the code below)  
> So my question is, whether there is a possibility to use ar[a…b][x…y]  
> = [[m, …], [n, …], …]. That means, how can I merge two  
> 2-dimensional arrays?

Define “merge.” Leave nothing to the imagination.

> field[pos\_x…(pos\_x + thing.length)][pos\_y…(pos\_y + thing.length)] =  
> thing

Well, you have succeeded in stating what you don’t understand. Now,  
please,  
just say what you want to accomplish. There is no question that you can  
do  
it using Ruby.

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [October 22, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/with-moredimensional-arrays/73194/3 "2006-10-22T18:36:07Z")

</div>

I’m sorry for my horrible English, I’m German.  
Anyway, of course I meant multidimensional and I want to get e.g.

0000 10 0100  
0000 and 01 to become 0010  
0000 11 0110

The first Array is definded by Array.new(3, Array.new(4, 0)), the  
others the same way.  
I hope you understand the problem now.

Thanks,  
naPOLeon

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [October 23, 2006, 10:31am UTC](https://www.ruby-forum.com/t/with-moredimensional-arrays/73194/4 "2006-10-23T10:31:55Z")

</div>

Thanks a lot, Paul! That’s exactly what I was up to.  
I started with Ruby two month ago, so still learning…

cu, naPOLeon

---

<div class="post-metadata">

**Author:** ![naPOLeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/58956e/32.png) [@naPOLeon](https://www.ruby-forum.com/u/naPOLeon)\
**Post date:** [October 22, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/with-moredimensional-arrays/73194/5 "2006-10-22T21:22:57Z")

</div>

naPOLeon wrote:

> I’m sorry for my horrible English, I’m German.

Your English is much better than my German, so no problem. All we have  
to do  
is figure out how to communicate the fine points of Ruby code, but  
without  
any code.

> Anyway, of course I meant multidimensional and I want to get e.g.
> 
> 0000 10 0100  
> 0000 and 01 to become 0010  
> 0000 11 0110
> 
> The first Array is definded by Array.new(3, Array.new(4, 0)), the  
> others the same way.  
> I hope you understand the problem now.

From the appearance of your example, it appears that you want to replace  
the  
contents of two middle columns in a 4x3 array with the contents of a 2x3  
array. Yes?

#!/usr/bin/ruby -w

array1 = [  
[0,0,0,0],  
[0,0,0,0],  
[0,0,0,0]  
]

array2 = [  
[1,0],  
[0,1],  
[1,1]  
]

array1.each\_index do |i|  
array1[i][1 … 2] = array2[i]  
end

p array1

output:

[[0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 1, 0]]

If this isn’t what you had in mind, we’ll have to try again.
