On Jul 14, 3:56=A0pm, Shawn W_ <sha... / internode.on.net> wrote:
> I have a 2D Array. I have written a method
> Array2D.adjacent(x,y,direction) that returns the adjacent cell to x,y in
> the direction given. How do I deal with the boundary conditions without
> receiving an error message.
>
> For example, if I refer to a cell on the top row, and look north there
> will be nothing there, and my program falls over.
>
> I will be creating methods that run over the whole 2D array, replacing
> things in random directions, so when it randomly hits a boundary I need
> my program to ignore cells outside the boundary.
>
> How can I do this? Thx
> --
> Posted viahttp://www.ruby-forum.com/.

Yields nil if out of bounds:

if ary[y]
  ary[y][x]
else
  nil
end