I'm making a game with a Board class Cell class. The Board class needs to be initialized with a unique instance variable for each Cell. I can hardcode it so that it works, but it seems inelegant and doesn't allow the size of the board to be chosen by the user at runtime. Here's what I have:
class Board
def initialize
@cell_1 = Cell.new(1)
@cell_2 = Cell.new(2)
@cell_3 = Cell.new(3)
@cell_4 = Cell.new(4)
@cell_5 = Cell.new(5)
@cell_6 = Cell.new(6)
@cell_7 = Cell.new(7)
@cell_8 = Cell.new(8)
@cell_9 = Cell.new(0)
@cells = [@cell_1, @cell_2, @cell_3,
@cell_4, @cell_5, @cell_6,
@cell_7, @cell_8, @cell_9]
end
end
I think I could use a loop to create a hash with unique key names pointing to unique Cell objects, but I don't know how I could make unique instance variables with a loop.