Ruby objects can define methods for equality and ordering. The commonly used `==` belongs to a family that also includes `!=`, `<`, `>`, `<=`, and `>=`; a class need not define every comparison if those comparisons do not make sense for its objects. section 7.6 section 7.6.2
To give a class the full comparison suite:
- Include Ruby’s `Comparable` module in the class. section 7.6.2
- Define the instance method `<=>` to compare two objects. Return a negative number for less than, zero for equal, or a positive number for greater than. section 7.6.2
Listing 7.2 Example of a class that mixes in the Comparable module
class Bid
include Comparable
attr_accessor :estimate
def <=>(other_bid) 1
if self.estimate < other_bid.estimate
-1
elsif self.estimate > other_bid.estimate
1
else
0
end
end
end
This Bid class compares objects by their estimates, letting bids use operators such as `<`. section 7.6.2







