I was going to suggest using the 'case' statement instead of all those =
elsifs, but then I realized there was an even better way.=20

class Array2D

	Delta=3D[[0,0], [0,1], [1,1], [1,0], [1,-1], [0,-1], [-1,-1], =
[-1,0], [-1,1]]
	attr_reader :width, :height

	def initialize(width, height)
		@width =3D width
		@height =3D height
		@data =3D Array.new(@width) { Array.new(@height) }
	end

	def [](x, y, z)
		deltaX, deltaY =3D *Delta[z]
		x =3D x + deltaX % @width=20
		y =3D y + deltaY
		@data[x][y] unless y<0 or y>@height=20
	end
=09
	def []=3D(x, y, z, value)
		deltaX, deltaY =3D *Delta[z]
		x =3D (x + deltaX) % @width # modulus % allows wrapping
		y =3D y + deltaY
		@data[x][y] =3D value unless y<0 or y>(@height-1)
	end
end


Obviously the Delta array takes the place of the elsif chains in both [] =
and []=3D. Also, :width and :height are defined with attr_reader, not =
attr_accessor, since it doesn't do any good at all to change those =
values after the array has been created.=20=