Module: Comparable

Included in:
Numeric, String, Time
Defined in:
opal/opal/corelib/comparable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize(what) ⇒ Object



2
3
4
5
6
7
8
# File 'opal/opal/corelib/comparable.rb', line 2

def self.normalize(what)
  return what if Integer === what

  return  1 if what > 0
  return -1 if what < 0
  return  0
end

Instance Method Details

#<(other) ⇒ Object



35
36
37
38
39
40
41
# File 'opal/opal/corelib/comparable.rb', line 35

def <(other)
  unless cmp = (self <=> other)
    raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
  end

  Comparable.normalize(cmp) < 0
end

#<=(other) ⇒ Object



43
44
45
46
47
48
49
# File 'opal/opal/corelib/comparable.rb', line 43

def <=(other)
  unless cmp = (self <=> other)
    raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
  end

  Comparable.normalize(cmp) <= 0
end

#==(other) ⇒ Object



10
11
12
13
14
15
16
17
# File 'opal/opal/corelib/comparable.rb', line 10

def ==(other)
  return true if equal?(other)

  return false unless cmp = (self <=> other)
  return Comparable.normalize(cmp) == 0
rescue StandardError
  false
end

#>(other) ⇒ Object



19
20
21
22
23
24
25
# File 'opal/opal/corelib/comparable.rb', line 19

def >(other)
  unless cmp = (self <=> other)
    raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
  end

  Comparable.normalize(cmp) > 0
end

#>=(other) ⇒ Object



27
28
29
30
31
32
33
# File 'opal/opal/corelib/comparable.rb', line 27

def >=(other)
  unless cmp = (self <=> other)
    raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
  end

  Comparable.normalize(cmp) >= 0
end

#between?(min, max) ⇒ Boolean

Returns:



51
52
53
54
55
# File 'opal/opal/corelib/comparable.rb', line 51

def between?(min, max)
  return false if self < min
  return false if self > max
  return true
end