From: Martin DeMello Date: 2010-07-18T17:43:07+09:00 Subject: Re: Return nothing when looking outside the bounds of 2D array? On Sat, Jul 17, 2010 at 8:14 AM, Dave Howell wrote: > OK, let's try this from the top. {grin} Here's some fresh new code: Looks good :) > Note that @data is no longer defined as "Array.new(@width) { Array.new(@height) }". I think I copied that from your code? but discovered that it gave each row the SAME array, so that changing [1][3] caused every cell in that column to have the same value. Useless! Not the first time that feature of Ruby (and it is a feature, wacky though it sometimes seems) has surprised me. That shouldn't have happened - the block passed to Array.new should create a new array every time it's called. irb(main):001:0> a = Array.new(5) { Array.new(3) } => [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] irb(main):002:0> a[1][2] = 0 => 0 irb(main):003:0> a => [[nil, nil, nil], [nil, nil, 0], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] irb(main):004:0> b = Array.new(5, Array.new(3)) => [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] irb(main):005:0> b[1][2] = 0 => 0 irb(main):006:0> b => [[nil, nil, 0], [nil, nil, 0], [nil, nil, 0], [nil, nil, 0], [nil, nil, 0]] martin