Module: Comparable

Included in:
Numeric
Defined in:
lib/source/ruby.rb

Overview

The Comparable mixin is used by classes whose objects may be ordered. The class must define the <=> 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")

s1 < s2                 #=> true
s4.between?(s1, s3)     #=> false
s3.between?(s2, s4)     #=> true
[s3, s2, s4, s1].sort   #=> [Z, YY, XXX, WWWW]

Instance Method Summary collapse

Instance Method Details

#<(obj) ⇒ Object

call-seq:

obj < other -> true or false

Compares two objects based on the receiver’s <=> method, returning true if the comparison returns -1.



1218
1219
1220
# File 'lib/source/ruby.rb', line 1218

def <(obj)
  `this.m$_ltgt(obj)==-1`
end

#<=(obj) ⇒ Object

call-seq:

obj <= other -> true or false

Compares two objects based on the receiver’s <=> method, returning true if the comparison returns -1 or 0.



1228
1229
1230
1231
# File 'lib/source/ruby.rb', line 1228

def <=(obj)
  `var result=this.m$_ltgt(obj)`
  `result==0||result==-1`
end

#==(obj) ⇒ Object

call-seq:

obj == other -> true or false

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



1240
1241
1242
# File 'lib/source/ruby.rb', line 1240

def ==(obj)
  `(this.__id__&&obj.__id__&&this.__id__==obj.__id__)||this.m$_ltgt(obj)==0`
end

#>(obj) ⇒ Object

call-seq:

obj > other -> true or false

Compares two objects based on the receiver’s <=> method, returning true if the comparison returns 1.



1250
1251
1252
# File 'lib/source/ruby.rb', line 1250

def >(obj)
  `this.m$_ltgt(obj)==1`
end

#>=(obj) ⇒ Object

call-seq:

obj >= other -> true or false

Compares two objects based on the receiver’s <=> method, returning true if it returns 1 or 0.



1259
1260
1261
1262
# File 'lib/source/ruby.rb', line 1259

def >=(obj)
  `var result=this.m$_ltgt(obj)`
  `result==0||result==1`
end

#between?(min, max) ⇒ Boolean

call-seq:

obj.between?(min,max) -> true or false

Returns false if obj <=> min is less than zero or if obj <=> max is greater than zero, true otherwise.

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

Returns:

  • (Boolean)


1275
1276
1277
1278
1279
# File 'lib/source/ruby.rb', line 1275

def between?(min, max)
  `if(this.m$_ltgt(min)==-1){return false;}`
  `if(this.m$_ltgt(max)==1){return false;}`
  return true
end