I am building a Chess program in Ruby and my Square objects are in a multi-dimensional array.
Example code for Square class:
class Square
attr_accessor :piece_on_square, :x, :y, :coordinates
def initialize(piece_on_square=nil, x=nil, y=nil, coordinates=nil)
@piece_on_square = piece_on_square
@x = x
@y = y
@coordinates = coordinates
end
end
Code within my Board class:
@square_array = Array.new(8){Array.new(8){Square.new}}
The problem comes when I try to select a Square object in the @square_array which matches a condition (such as a Square with coordinates of "a4"). I've tried using nested #each_with_index calls with #select but that isn't working. I've only been able to select the array itself, not the object in the array. What's the best way to do this?
arr = [];arr[10] = "Hi";p arr