Module: Comparable

Included in:
Numeric, String, Symbol
Defined in:
mruby/mrblib/compar.rb,
mruby/mrbgems/mruby-compar-ext/mrblib/compar.rb

Overview

Comparable

ISO 15.3.3

Instance Method Summary collapse

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 'mruby/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 'mruby/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 'mruby/mrblib/compar.rb', line 50

def == other
  cmp = self <=> other
  cmp == 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 'mruby/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 'mruby/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

Returns:

  • (Boolean)


99
100
101
# File 'mruby/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
75
76
77
78
79
80
81
82
# File 'mruby/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?
        max = self
      elsif min.exclude_end?
        raise ArgumentError, "cannot clamp with an exclusive range"
      end
      min = min.begin
      if min.nil?
        min = self
      end
    else
      raise TypeError, "wrong argument type #{min.class}"
    end
  end
  c = min <=> max
  if c.nil?
    raise ArgumentError, "comparison of #{min.class} with #{max.class} failed"
  elsif c > 0
    raise ArgumentError, "min argument must be smaller than max argument"
  end
  c = self <=> min
  if c.nil?
    raise ArgumentError, "comparison of #{self.class} with #{min.class} failed"
  elsif c == 0
    return self
  elsif c < 0
    return min
  end
  c = self <=> max
  if c.nil?
    raise ArgumentError, "comparison of #{self.class} with #{max.class} failed"
  elsif c > 0
    return max
  else
    return self
  end
end