doug meyer
5/21/2007 8:19:00 PM
I assume you meant to include this:
square = Array.new(rows){Array.new(rows)}
On 5/21/07, James Edward Gray II <james@grayproductions.net> wrote:
> On May 21, 2007, at 1:58 PM, Yossef Mendelssohn wrote:
>
> > On May 21, 9:26 am, "doug meyer" <doug.me...@sigeps.org> 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
>
>