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
ifc
is less thanother
(i.e.,c
<=>
other
returns a negative number).c
<=
other
Returns
true
ifc
is less than or equal toother
(i.e.,c
<=>
other
returns either a negative number or0
).c
>
other
Returns
true
ifc
is greater thanother
(i.e.,c
<=>
other
returns a positive number).c
>=
other
Returns
true
ifc
is greater than or equal toother
(i.e.,c
<=>
other
returns either a positive number or0
).c
== other
Returns
true
if the objects are equal (i.e.,c
<=>
other
returns0
).c
.between?(
min
,
max
)
Returns
true
ifc
is betweenmin
andmax
.
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.