From: Markus Date: 2004-09-23T08:05:44+09:00 Subject: Re: Newbie: Case statement > So my underlying question is:- what's the fastest Ruby for checking > which of the 8 possible adjacent cells on a board (2D array) are > still "on the board" given x,y: x_range = (x=min_x ? x : x-1)..(x=max_x ? x : x+1) y_range = (y=min_y ? y : y-1)..(y=max_y ? y : y+1) x_range.collect { |x0| y_range.collect { |y0| [x0,y0] unless x0 == x and y0 == y} }.compact might do what you want. Depends on what you mean by fast. -- Markus P.S. In use, this might be something more like: class A_chess_board attr_reader :min_x,:min_y,:max_x,:max_y def for_each_neighbor_of(x,y) x_range = (x=min_x ? x : x-1)..(x=max_x ? x : x+1) y_range = (y=min_y ? y : y-1)..(y=max_y ? y : y+1) x_range.collect { |x0| y_range.collect { |y0| yield(x0,y0) unless x0 == x and y0 == y} } } } end