Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTeri Dent
11,515 PointsA list of questions
I am not even sure where to begin with this…
“The comparable module is a mix-in”
-What is a mix-in? Is this an actual reserved term for something tangible in Ruby or just a colloquialism that the teacher uses? If it is a reserved term, has it been defined in a previous video?
The comparable class “It's used by classes whose objects may be ordered”.
In the example it seems as if the greater than “>” functionality does not work, which seems odd because:
- Jason has said that most everything in ruby is an object
- When I want to compare 5 > 2 I don’t need to include the comparable module and it works fine, so…
- What about this case requires that I add the module?
Following from that, the def <=> code can be replaced by an if statement and you get the same result.
This:
def <=>(other_player)
score <=> other_player.score
end
Gives the same result as this:
if player1.score > player2.score
puts "player 1 > player 2: true"
else
puts "player 1 < player 2 : true"
end
The marginal savings in typing and performance is minimal at best, and doesn’t require any module inclusions, so what am I missing?
“And the nice thing about this is, we didn't have to write any code to see whether or not this was greater than something else”
Unless I am missing something, this is demonstrably false. The comparable documentation can be used for several different kinds of comparisons, so you MUST supply some code to tell the module which comparison to perform, which is (I think) shown in the final two lines:
puts "player1 > player2: %s" % (player1 > player2)
puts "player1 < player2: %s" % (player1 < player2)
Going back to the idea above, how can you say that the comparible method “saves” anything at all when you must not only provide the kind of comparison that the module will handle for you, but also define an implementation of the spaceship operator as well?
Speaking of the spaceship method:
def <=>(other_player)
score <=> other_player.score
end
The right side of the spaceship refers to the score instance variable, but what about the left side score? How does it know to refer to the “other score” of the object? Is it sufficient to define one item of comparison (in this case the value stored in the score instance variable) and ruby is smart enough to know to look for the same instance variable (same variable name maybe?) of the other object it is comparing? A visual walkthrough of how this works would be helpful.