[]= with moredimensional Arrays

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

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,
just say what you want to accomplish. There is no question that you can
do
it using Ruby.

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

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

cu, naPOLeon

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.