Module: Comparable
- Defined in:
- mrblib/compar.rb,
mrbgems/mruby-compar-ext/mrblib/compar.rb
Overview
Comparable
ISO 15.3.3
Instance Method Summary collapse
-
#<(other) ⇒ Object
call-seq: obj < other -> true or false.
-
#<=(other) ⇒ Object
call-seq: obj <= other -> true or false.
-
#==(other) ⇒ Object
call-seq: obj == other -> true or false.
-
#>(other) ⇒ Object
call-seq: obj > other -> true or false.
-
#>=(other) ⇒ Object
call-seq: obj >= other -> true or false.
-
#between?(min, max) ⇒ Boolean
call-seq: obj.between?(min,max) -> true or false.
-
#clamp(min, max = nil) ⇒ Object
call-seq: obj.clamp(min, max) -> obj obj.clamp(range) -> obj.
Instance Method Details
#<(other) ⇒ Object
call-seq: obj < other -> true or false
Return true if self is less
than other. Otherwise return
false.
ISO 15.3.3.2.1
16 17 18 19 20 21 22 |
# File 'mrblib/compar.rb', line 16 def < other cmp = self <=> other if cmp.nil? raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end cmp < 0 end |
#<=(other) ⇒ Object
call-seq: obj <= other -> true or false
Return true if self is less
than or equal to other.
Otherwise return false.
ISO 15.3.3.2.2
33 34 35 36 37 38 39 |
# File 'mrblib/compar.rb', line 33 def <= other cmp = self <=> other if cmp.nil? raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end cmp <= 0 end |
#==(other) ⇒ Object
call-seq: obj == other -> true or false
Return true if self is equal
to other. Otherwise return
false.
ISO 15.3.3.2.3
50 51 52 53 |
# File 'mrblib/compar.rb', line 50 def == other cmp = self <=> other cmp.equal?(0) end |
#>(other) ⇒ Object
call-seq: obj > other -> true or false
Return true if self is greater
than other. Otherwise return
false.
ISO 15.3.3.2.4
64 65 66 67 68 69 70 |
# File 'mrblib/compar.rb', line 64 def > other cmp = self <=> other if cmp.nil? raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end cmp > 0 end |
#>=(other) ⇒ Object
call-seq: obj >= other -> true or false
Return true if self is greater
than or equal to other.
Otherwise return false.
ISO 15.3.3.2.5
81 82 83 84 85 86 87 |
# File 'mrblib/compar.rb', line 81 def >= other cmp = self <=> other if cmp.nil? raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end cmp >= 0 end |
#between?(min, max) ⇒ Boolean
call-seq: obj.between?(min,max) -> true or false
Return true if self is greater
than or equal to min and
less than or equal to max.
Otherwise return false.
ISO 15.3.3.2.6
99 100 101 |
# File 'mrblib/compar.rb', line 99 def between?(min, max) self >= min and self <= max end |
#clamp(min, max = nil) ⇒ Object
call-seq: obj.clamp(min, max) -> obj obj.clamp(range) -> obj
In (min, max) form, returns min if obj
<=> min is less than zero, max if obj
<=> 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'
In (range) form, returns range.begin if obj
<=> range.begin is less than zero, range.end
if obj <=> range.end 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 range.begin is nil, it is considered smaller than obj,
and if range.end is nil, it is considered greater than
obj.
-20.clamp(0..) #=> 0
523.clamp(..100) #=> 100
When range.end is excluded and not nil, an exception is
raised.
100.clamp(0...100) # ArgumentError
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'mrbgems/mruby-compar-ext/mrblib/compar.rb', line 43 def clamp(min, max=nil) if max.nil? if min.kind_of?(Range) max = min.end if !max.nil? && min.exclude_end? raise ArgumentError, "cannot clamp with an exclusive range" end min = min.begin end end if !min.nil? && !max.nil? cmp = min <=> max if cmp.nil? raise ArgumentError, "comparison of #{min.class} with #{max.class} failed" elsif cmp > 0 raise ArgumentError, "min argument must be smaller than max argument" end end unless min.nil? cmp = self <=> min return self if cmp == 0 return min if cmp < 0 end unless max.nil? cmp = self <=> max return max if cmp > 0 end return self end |