[]= with moredimensional Arrays

N

naPOLeon

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
 
N

naPOLeon

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
 
N

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top