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:
self
is less than the other objectself
is greater than the other object0
if self
is equal to the other objectnil
if self
and the other object are not comparableComparable
uses #<=>
to implement the conventional comparison operators (#<
, #<=
, #==
, #>=
, and #>
). All of these return false
when #<=>
returns nil
.
Note that returning nil
is only useful when defining a partial comparable relationship. One such example is float values: they are generally comparable, except for NaN
. If none of the values of a type are comparable between each other, Comparable
shouldn't be included.
NOTE When nil
is returned from #<=>
, Array#sort
and related sorting methods will perform slightly slower.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a negative number.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a value equal or less then 0
.
The comparison operator.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns 0
.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a value greater then 0
.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a value equal or greater than 0
.
Clamps a value between min and max.
Clamps a value within range.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a negative number.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a value equal or less then 0
.
The comparison operator. Returns 0
if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greter than other, or nil
if the two objects are not comparable.
Subclasses define this method to provide class-specific ordering.
The comparison operator is usually used to sort values:
# Sort in a descending way: [3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1] # Sort in an ascending way: [3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
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 a value greater then 0
.
Compares this object to other based on the receiver’s #<=>
method, returning true
if it returns a value equal or greater than 0
.
Clamps a value between min and max.
5.clamp(10, 100) # => 10 50.clamp(10, 100) # => 50 500.clamp(10, 100) # => 100 5.clamp(10, nil) # => 10 50.clamp(10, nil) # => 50 5.clamp(nil, 10) # => 5 50.clamp(nil, 10) # => 10
Clamps a value within range.
5.clamp(10..100) # => 10 50.clamp(10..100) # => 50 500.clamp(10..100) # => 100 5.clamp(10..) # => 10 50.clamp(10..) # => 50 5.clamp(..10) # => 5 50.clamp(..10) # => 10
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Comparable.html