module Comparable(T)
Overview
The Comparable
mixin is used by classes whose objects may be ordered.
Including types must provide an #<=>
method, which compares the receiver against
another object, returning -1
, 0
, or +1
depending on whether the receiver is less than,
equal to, or greater than the other object.
Comparable
uses #<=>
to implement the conventional comparison operators (#<
, #<=
, #==
, #>=
, and #>
).
Direct including types
- Array(T)
- BigFloat
- BigInt
- BigRational
- Char
- Deque(T)
- Enum
- Float
- Int
- Number
- Pointer(T)
- String
- Symbol
- Time
- Time::Span
- Tuple(*T)
Defined in:
comparable.crInstance Method Summary
-
#<(other : T)
Compares this object to other based on the receiver’s
#<=>
method, returningtrue
if it returns-1
. -
#<=(other : T)
Compares this object to other based on the receiver’s
#<=>
method, returningtrue
if it returns-1
or0
. -
#<=>(other : T)
Comparison operator.
-
#==(other : T)
Compares this object to other based on the receiver’s
#<=>
method, returningtrue
if it returns0
. -
#>(other : T)
Compares this object to other based on the receiver’s
#<=>
method, returningtrue
if it returns1
. -
#>=(other : T)
Compares this object to other based on the receiver’s
#<=>
method, returningtrue
if it returns1
or0
.
Instance Method Detail
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns -1
.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns -1
or 0
.
Comparison operator. Returns 0
if the two objects are equal,
a negative number if this object is considered less than other,
or a positive number otherwise.
Subclasses define this method to provide class-specific ordering.
# Sort in a descending way
[4, 7, 2].sort { |x, y| y <=> x } # => [7, 4, 2]
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns 0
.
Also returns true
if this and other are the same object.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns 1
.