From: dblack@... Date: 2002-11-09T08:38:50+09:00 Subject: Re: Multi-dimensional (like 2) arrays in Ruby Hi -- On Sat, 9 Nov 2002, Austin Ziegler wrote: > On Sat, 9 Nov 2002 08:18:31 +0900, Ted wrote: > > All of the replies say the same thing -- this is good. My learning > > project for Ruby is implementing a turning grille cipher. I want > > to allocate a n*m matrix where n and m are read from stdin. > > > > I have the n[] (the rows) array, no sweat. But the m part (the > > columns) is rather elusive. > > > > Here's my latest code: > > > > @m = Array.new(@rows) > > @M = @m.each {|row| @m[row] = Array.new(@cols)} > > Try: > > matrix = Array.new(@rows, Array.new(@cols)) The problem with that is that each row ends up being the same object: cols,rows = 3,5 m = Array.new(rows, Array.new(cols)) m[1][1] = "hello" p m[2][1] # => "hello" I'm not sure whether there's a plan afoot to enable a proc-calling version of this (with which you could create a separate object each time). David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav