Name

Comparable — Comparable mix-in module

Synopsis

The Comparable module assumes that the including class has a <=> method defined. The <=> method compares two objects and returns a positive number if the left operand is greater, 0 if it’s equal to the right operand, or a negative number if it’s smaller. You can add the following methods to a class that provides <=>, by just including this module.

Instance Methods

c < other

Returns true if c is less than other (i.e., c <=> other returns a negative number).

c <= other

Returns true if c is less than or equal to other (i.e., c <=> other returns either a negative number or 0).

c > other

Returns true if c is greater than other (i.e., c <=> other returns a positive number).

c >= other

Returns true if c is greater than or equal to other (i.e., c <=> other returns either a positive number or 0).

c == other

Returns true if the objects are equal (i.e., c <=> other returns 0).

c.between?(min, max)

Returns true if c is between min and max.

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.