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 trial

Ruby Ruby Modules Ruby Core Modules Comparable

Hi! I'm curios for a little more explanation about the spaceship operator and why one has to define it in the class...

My questions are : (1) where does the value of the argument "other_player" come from. (2) Why do we name the method "(spaceship operator symbol)" and then call it like "objt > othrObjt". (3) Why are we required to define the method in the first place.

I would love to hear any insight anyone can provide!

Thanks!

Garrett Carver
Garrett Carver
14,681 Points

I am a bit confused by this also. My guess is that other_player in the method for the spaceship operator comes from the statement:
puts "player1 > player2: %s" % (player1 > player2)

It almost like it's calling the spaceship method like this instead:
puts "player1 > player2: %s" % (player1.<=>(player2))

Ah! That's interesting! That would make a lot of sense... Thank you very much!

yeah I get the notation and syntax. But I still don't get how <=> would compensate for > or <. or is it just specifying what these methods will do, and then use <=> from the comparable module to override comparable Module's implementaiotn of <=> to suit the classes's needs.