I have been tasked to create an unbeatable tic tac toe opponent for a coding challenge, and I would like to create a Ruby gem to do so, which has proper test coverage and decent Object Oriented Design (OOD).
Having never made my own gem before, and being a new student of proper OOD principles, I found a good blog post that walks through exactly what I need : http://codequizzes.wordpress.com/2013/10/25/creating-a-tic-tac-toe-game-with-ruby/
In defining the Cell class, the following code is given as an example:
module TicTacToe
class Cell
attr_accessor :value
def initialize (value = "")
@value = value
end
end
end
It seems to me, though, that given the simplicity of this initialization, we could just as easily do this:
module TicTacToe
class Cell
attr_accessor :value
def initialize
@value = ""
end
end
end
So what's the argument for doing it the first way over the second?
EDIT
Okay, I feel a bit silly now; reading the blog post a bit closer, it clearly says
The Cell class is wrapped in a TicTacToe module to follow Ruby gem conventions and prevent >class name collisions when gems are included in other projects. If Cell is initialized >without any arguments, the cell’s value will be the empty string, but Cell can also be >initialized with an argument. After a cell is instantiated, its value cannot be updated.
However, I am still confused about the last sentence, "After a cell is instantiated, its value cannot be updated."
I would think that is incorrect in this example, as to my understanding, the attr_accessor method makes value both readable and writable -- as it is writable, couldn't I update it by saying
move = Cell.new
move.value = X
""the default value forvalue, sovalue => ""forCell.new, butvalue => "cat"forCell.new("cat").