module Comparable(T)
Overview
TheComparable mixin is used by classes whose objects may be ordered.
Including types must provide an<=> method, which compares the receiver against
another object, returning:
- a negative number if
selfis less than the other object - a positive number if
selfis greater than the other object 0ifselfis equal to the other objectnilifselfand the other object are not comparable
Comparable uses<=> to implement the conventional comparison operators
(<,<=,#==,>=, and>). All of these returnfalse when<=>
returnsnil.
Note that returningnil is only useful when defining a partial comparable
relationship. One such example is float values: they are generally comparable,
except forNaN. If none of the values of a type are comparable between each
other,Comparable shouldn't be included.
NOTE Whennil is returned from<=>,Array#sort and related sorting
methods will perform slightly slower.
Direct including types
- Array(T)
- BigDecimal
- BigFloat
- BigInt
- BigRational
- Char
- Enum
- Float
- Int
- Number
- Path
- Pointer(T)
- SemanticVersion
- SemanticVersion::Prerelease
- Slice(T)
- StaticArray(T, N)
- String
- Symbol
- Time
- Time::Instant
- Time::Span
- Tuple(*T)
- UUID
Defined in:
comparable.crInstance Method Summary
-
#<(other : T) : Bool
Compares this object toother based on the receiver’s
<=>method, returningtrueif it returns a negative number. -
#<=(other : T)
Compares this object toother based on the receiver’s
<=>method, returningtrueif it returns a value equal or less then0. -
#<=>(other : T)
The comparison operator.
-
#==(other : T)
Compares this object toother based on the receiver’s
<=>method, returningtrueif it returns0. -
#>(other : T) : Bool
Compares this object toother based on the receiver’s
<=>method, returningtrueif it returns a value greater then0. -
#>=(other : T)
Compares this object toother based on the receiver’s
<=>method, returningtrueif it returns a value equal or greater than0. -
#clamp(min, max)
Clamps a value betweenmin andmax.
-
#clamp(range : Range)
Clamps a value withinrange.
Instance Method Detail
Compares this object toother based on the receiver’s<=> method,
returningtrue if it returns a negative number.
Compares this object toother based on the receiver’s<=> method,
returningtrue if it returns a value equal or less then0.
The comparison operator. Returns0 if the two objects are equal,
a negative number if this object is considered less thanother,
a positive number if this object is considered greater thanother,
ornil 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 toother based on the receiver’s<=> method,
returningtrue if it returns0.
Also returnstrue if this andother are the same object.
Compares this object toother based on the receiver’s<=> method,
returningtrue if it returns a value greater then0.
Compares this object toother based on the receiver’s<=> method,
returningtrue if it returns a value equal or greater than0.
Clamps a value betweenmin andmax.
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