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



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

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

  `#{Comparable.normalize(cmp)} < 0`
end

#<=(other) ⇒ Object



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

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
18
# 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



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

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

  `#{Comparable.normalize(cmp)} > 0`
end

#>=(other) ⇒ Object



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

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:



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

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