There is an error in your code. Instead of cell = Class.new
you should do cell = Cell.new
.
Yes you can pass the board (self) as a parameter in the Cell's constructor (initialize). In fact is more clean and functional in that way. Check out this code:
class Board
def create_cell
cell = Cell.new(self)
end
end
class Cell
attr_accessor :board
def initialize board
@board = board
end
end
And then some examples of use.
$> b = Board.new
# => #<Board:0x000001021c0298>
$> c1 = b.create_cell
# => #<Cell:0x000001021c27a0 @board=#<Board:0x000001021c0298>>
$> c2 = b.create_cell
# => #<Cell:0x000001021d4270 @board=#<Board:0x000001021c0298>>
$> c2.board == c1.board
# => true
As you can notice, either cell.board = self
or using the constructor
(initialize), is setting the current board instance into the created cell. So all these cells will point to that board.