On Sep 11, 2007, at 2:03 PM, Morton Goldberg wrote: > <code> > def each_with_neighbor > each { |cell| cell.visited = false } > each do |cell| > next if cell.locked > neighbors(cell).each { |other| yield cell, other } > cell.visited = true > end > end > > def neighbors(cell) > result = [] > x, y = cell.x, cell.y > (-1..1).each do |dx| > (-1..1).each do |dy| > begin > next if dx == 0 && dy == 0 > _cell = cell.grid[x+dx, y+dy] > result << _cell unless _cell.locked || _cell.visited > rescue IndexError > next > end > end > end > result > end > </code> As is not unusual for me, I've had second thoughts. Here is a better version. <code> def each_with_neighbor each do |cell| next if cell.locked neighbors(cell).each { |other| yield cell, other } end end DELTAS = [[1, 0], [-1, 1], [0, 1], [1, 1]] def neighbors(cell) result = [] x, y = cell.x, cell.y DELTAS.each do |dx, dy| begin _cell = cell.grid[x+dx, y+dy] result << _cell unless _cell.locked rescue IndexError next end end result end </code> Regards, Morton