Module: Comparable

Included in:
Laser::Analysis::Bindings::Base, Laser::Analysis::Signature, Laser::Frequency, Numeric, String
Defined in:
lib/laser/standard_library/comparable.rb

Overview

LICENSING: Taken from Rubinius. Retrieved from github.com/rubinius/rubinius/blob/master/kernel/common/comparable.rb on 7/25/2011 commit de03069d21839235fd59

The Comparable mixin is used by classes whose objects may be ordered. The class must define the <=> (spaceship) operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object.

Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method between?.

class SizeMatters
  include Comparable
  attr :str
  def <=>(other)
    str.size <=> other.str.size
  end
  def initialize(str)
    @str = str
  end
  def inspect
    @str
  end
end

s1 = SizeMatters.new "Z"
s2 = SizeMatters.new "YY"
s3 = SizeMatters.new "XXX"
s4 = SizeMatters.new "WWWW"
s5 = SizeMatters.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]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compare_int(int) ⇒ Object

A version of MRI's rb_cmpint (sort of)



118
119
120
121
122
123
124
# File 'lib/laser/standard_library/comparable.rb', line 118

def self.compare_int(int)
  return int if int.kind_of? Fixnum

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

Instance Method Details

#<(other) ⇒ Object

Compares two objects based on the receiver's <=>

method, returning true if it returns -1.


82
83
84
85
86
87
88
# File 'lib/laser/standard_library/comparable.rb', line 82

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

  Comparable.compare_int(comp) < 0
end

#<=(other) ⇒ Object

Compares two objects based on the receiver's <=>

method, returning true if it returns -1 or 0.


92
93
94
95
96
97
98
# File 'lib/laser/standard_library/comparable.rb', line 92

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

  Comparable.compare_int(comp) <= 0
end

#==(other) ⇒ Object

Compares two objects based on the receiver's <=> method, returning true if it returns 0. Also returns true if obj and other are the same object.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/laser/standard_library/comparable.rb', line 46

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

  begin
    unless comp = (self <=> other)
      return nil
    end

    return Comparable.compare_int(comp) == 0
  rescue StandardError
    return nil
  end
end

#>(other) ⇒ Object

Compares two objects based on the receiver's <=>

method, returning true if it returns 1.


62
63
64
65
66
67
68
# File 'lib/laser/standard_library/comparable.rb', line 62

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

  Comparable.compare_int(comp) > 0
end

#>=(other) ⇒ Object

Compares two objects based on the receiver's <=>

method, returning true if it returns 0 or 1.


72
73
74
75
76
77
78
# File 'lib/laser/standard_library/comparable.rb', line 72

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

  Comparable.compare_int(comp) >= 0
end

#between?(min, max) ⇒ Boolean

Returns false if obj <=>

<i>min</i> is less than zero or if <i>anObject</i> <code><=></code>
<i>max</i> is greater than zero, <code>true</code> otherwise.

   3.between?(1, 5)               #=> true
   6.between?(1, 5)               #=> false
   'cat'.between?('ant', 'dog')   #=> true
   'gnu'.between?('ant', 'dog')   #=> false

Returns:

  • (Boolean)


109
110
111
112
113
114
115
# File 'lib/laser/standard_library/comparable.rb', line 109

def between?(min, max)
  # This could be more elegant, but we need to use <= and => on self to match
  # MRI.
  return false if self < min
  return false if self > max
  return true
end