| # <!-- rdoc-file=compar.c --> | |
| # The Comparable mixin is used by classes whose objects may be ordered. The | |
| # class must define the <code><=></code> operator, which compares the receiver | |
| # against another object, returning a value less than 0, returning 0, or | |
| # returning a value greater than 0, depending on whether the receiver is less | |
| # than, equal to, or greater than the other object. If the other object is not | |
| # comparable then the <code><=></code> operator should return `nil`. Comparable | |
| # uses <code><=></code> to implement the conventional comparison operators | |
| # (<code><</code>, <code><=</code>, <code>==</code>, <code>>=</code>, and | |
| # <code>></code>) and the method <code>between?</code>. | |
| # | |
| # class StringSorter | |
| # include Comparable | |
| # | |
| # attr :str | |
| # def <=>(other) | |
| # str.size <=> other.str.size | |
| # end | |
| # | |
| # def initialize(str) | |
| # @str = str | |
| # end | |
| # | |
| # def inspect | |
| # @str | |
| # end | |
| # end | |
| # | |
| # s1 = StringSorter.new("Z") | |
| # s2 = StringSorter.new("YY") | |
| # s3 = StringSorter.new("XXX") | |
| # s4 = StringSorter.new("WWWW") | |
| # s5 = StringSorter.new("VVVVV") | |
| # | |
| # s1 < s2 #=> true | |
| # s4.between?(s1, s3) #=> false | |
| # s4.between?(s3, s5) #=> true | |
| # [ s3, s2, s5, s4, s1 ].sort #=> [Z, YY, XXX, WWWW, VVVVV] | |
| # | |
| # ## What's Here | |
| # | |
| # Module Comparable provides these methods, all of which use method | |
| # <code>#<=></code>: | |
| # | |
| # * #<: Returns whether `self` is less than the given object. | |
| # * #<=: Returns whether `self` is less than or equal to the given object. | |
| # * #==: Returns whether `self` is equal to the given object. | |
| # * #>: Returns whether `self` is greater than the given object. | |
| # * #>=: Returns whether `self` is greater than or equal to the given object. | |
| # * #between?: Returns `true` if `self` is between two given objects. | |
| # * #clamp: For given objects `min` and `max`, or range | |
| # <code>(min..max)</code>, returns: | |
| # | |
| # * `min` if <code>(self <=> min) < 0</code>. | |
| # * `max` if <code>(self <=> max) > 0</code>. | |
| # * `self` otherwise. | |
| # | |
| module Comparable : _WithSpaceshipOperator | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - self < other -> true or false | |
| # --> | |
| # Returns whether `self` is "less than" `other`; equivalent to <code>(self <=> | |
| # other) < 0</code>: | |
| # | |
| # 'foo' < 'foo' # => false | |
| # 'foo' < 'food' # => true | |
| # | |
| def <: (untyped other) -> bool | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - self <= other -> true or false | |
| # --> | |
| # Returns whether `self` is "less than or equal to" `other`; equivalent to | |
| # <code>(self <=> other) <= 0</code>: | |
| # | |
| # 'foo' <= 'foo' # => true | |
| # 'foo' <= 'food' # => true | |
| # 'food' <= 'foo' # => false | |
| # | |
| def <=: (untyped other) -> bool | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - obj == other -> true or false | |
| # --> | |
| # Compares two objects based on the receiver's <code><=></code> method, | |
| # returning true if it returns 0. Also returns true if *obj* and *other* are the | |
| # same object. | |
| # | |
| def ==: (untyped other) -> bool | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - obj > other -> true or false | |
| # --> | |
| # Compares two objects based on the receiver's <code><=></code> method, | |
| # returning true if it returns a value greater than 0. | |
| # | |
| def >: (untyped other) -> bool | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - obj >= other -> true or false | |
| # --> | |
| # Compares two objects based on the receiver's <code><=></code> method, | |
| # returning true if it returns a value greater than or equal to 0. | |
| # | |
| def >=: (untyped other) -> bool | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - obj.between?(min, max) -> true or false | |
| # --> | |
| # Returns `false` if *obj* <code><=></code> *min* is less than zero or if *obj* | |
| # <code><=></code> *max* is greater than zero, `true` otherwise. | |
| # | |
| # 3.between?(1, 5) #=> true | |
| # 6.between?(1, 5) #=> false | |
| # 'cat'.between?('ant', 'dog') #=> true | |
| # 'gnu'.between?('ant', 'dog') #=> false | |
| # | |
| def between?: (untyped min, untyped max) -> bool | |
| # <!-- | |
| # rdoc-file=compar.c | |
| # - obj.clamp(min, max) -> obj | |
| # - obj.clamp(range) -> obj | |
| # --> | |
| # In <code>(min, max)</code> form, returns *min* if *obj* <code><=></code> *min* | |
| # is less than zero, *max* if *obj* <code><=></code> *max* is greater than zero, | |
| # and *obj* otherwise. | |
| # | |
| # 12.clamp(0, 100) #=> 12 | |
| # 523.clamp(0, 100) #=> 100 | |
| # -3.123.clamp(0, 100) #=> 0 | |
| # | |
| # 'd'.clamp('a', 'f') #=> 'd' | |
| # 'z'.clamp('a', 'f') #=> 'f' | |
| # | |
| # If *min* is `nil`, it is considered smaller than *obj*, and if *max* is `nil`, | |
| # it is considered greater than *obj*. | |
| # | |
| # -20.clamp(0, nil) #=> 0 | |
| # 523.clamp(nil, 100) #=> 100 | |
| # | |
| # In <code>(range)</code> form, returns <em>range.begin</em> if *obj* | |
| # <code><=></code> <em>range.begin</em> is less than zero, <em>range.end</em> if | |
| # *obj* <code><=></code> <em>range.end</em> is greater than zero, and *obj* | |
| # otherwise. | |
| # | |
| # 12.clamp(0..100) #=> 12 | |
| # 523.clamp(0..100) #=> 100 | |
| # -3.123.clamp(0..100) #=> 0 | |
| # | |
| # 'd'.clamp('a'..'f') #=> 'd' | |
| # 'z'.clamp('a'..'f') #=> 'f' | |
| # | |
| # If <em>range.begin</em> is `nil`, it is considered smaller than *obj*, and if | |
| # <em>range.end</em> is `nil`, it is considered greater than *obj*. | |
| # | |
| # -20.clamp(0..) #=> 0 | |
| # 523.clamp(..100) #=> 100 | |
| # | |
| # When <em>range.end</em> is excluded and not `nil`, an exception is raised. | |
| # | |
| # 100.clamp(0...100) # ArgumentError | |
| # | |
| def clamp: [A, B] (A min, B max) -> (self | A | B) | |
| | [A] (Range[A]) -> (self | A) | |
| end | |
| # This interface defines the condition for Comparable mixin. | |
| # | |
| interface Comparable::_WithSpaceshipOperator | |
| # `<=>` operator must return Integer or `nil`. | |
| # If `other` is greater than `self`, it returns a positive Integer. | |
| # If `other` equals to `self`, it returns zero. | |
| # If `other` is less than `self`, it returns a positive Integer. | |
| # If no comparison is defined with `other` and `self`, it returns `nil`. | |
| # | |
| def <=>: (untyped other) -> Comparable::_CompareToZero? | |
| end | |
| # This interface indicates a type is comparable against zero. | |
| # | |
| interface Comparable::_CompareToZero | |
| def <: (0) -> boolish | |
| def >: (0) -> boolish | |
| end |