If someone has the time, could someone clarify how 2 methods work? The methods are the 2 indexing through the grid. I understand the bottom one is a setter
while the top is a getter
but why do I need the getter
in order to use the setter
? I tried to set a position on the grid to a symbol without the getter
defined and it didn’t work. Why is that?
Also, in my initialize
method, why does class
need to be attached to self
in order to call default_grid
? This question isn’t too important because I can live with “that’s the way it is” but if there is a deeper understanding I can gain, that would be great
class Board
attr_reader :grid
def initialize(grid = self.class.default_grid)
@grid = grid
end
def self.default_grid
Array.new(10){Array.new(10)}
end
def [](pos) <<<<< Getter
row, col = pos
grid[row][col]
end
def []=(pos, sym) <<<<<<Setter
x, y = pos
grid[x][y] = sym
end
end