Comparable
Rectangle comparable by area
Section titled “Rectangle comparable by area”Comparable is one of the most popular modules in Ruby. Its purpose is to provide with convenience comparison methods.
To use it, you have to include Comparable and define the space-ship operator (<=>):
class Rectangle include Comparable
def initialize(a, b) @a = a @b = b end
def area @a * @b end
def <=>(other) area <=> other.area endend
r1 = Rectangle.new(1, 1)r2 = Rectangle.new(2, 2)r3 = Rectangle.new(3, 3)
r2 >= r1 # => truer2.between? r1, r3 # => truer3.between? r1, r2 # => falseSyntax
Section titled “Syntax”include Comparable- implement the space-ship operator (
<=>)
Parameters
Section titled “Parameters”|Parameter|Details
|---|---|---|---|---|---|---|---|---|---
|other|The instance to be compared to self
Remarks
Section titled “Remarks”x <=> y should return a negative number if x < y, zero if x == y and a positive number if x > y.