From: doug meyer Date: 2007-05-22T05:18:45+09:00 Subject: Re: Magic Squares (#124) I assume you meant to include this: square = Array.new(rows){Array.new(rows)} On 5/21/07, James Edward Gray II wrote: > On May 21, 2007, at 1:58 PM, Yossef Mendelssohn wrote: > > > On May 21, 9:26 am, "doug meyer" wrote: > >> Hey, > >> Did anyone else run into the problem of trying to setup your > >> square using: > >> Array.new(size, Array.new(size)) > >> that was annoying! And I wasted too much time with formating the > >> output. > > > > If you're talking about the problem I think you're talking about, > > that's the reason I dismissed that approach out of hand. > > > > irb(main):001:0> x = Array.new(5, Array.new(5,0)) > > => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, > > 0], [0, 0, 0, 0, 0]] > > irb(main):002:0> x[2][2] = 5 > > => 5 > > irb(main):003:0> x > > => [[0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0, 0], [0, 0, 5, 0, > > 0], [0, 0, 5, 0, 0]] > > > > Those references will get you if you're not careful. > > But it's very easy to fix: > > => [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, > nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]] > >> square[2][2] = 5 > => 5 > >> square > => [[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, > 5, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]] > > James Edward Gray II > >