Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns:
  • -1, if self is less than other.
  • 0, if self is equal to other.
  • 1, if self is greater then other.
  • nil, if self and other are incomparable.
Examples:
1 <=> 2              # => -1
1 <=> 1              # => 0
1 <=> 0              # => 1
1 <=> 'foo'          # => nil

1 <=> 1.0            # => 0
1 <=> Rational(1, 1) # => 0
1 <=> Complex(1, 0)  # => 0
This method is the basis for comparisons in module Comparable.
 
               VALUE
rb_int_cmp(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_cmp(x, y);
    }
    else if (RB_BIGNUM_TYPE_P(x)) {
        return rb_big_cmp(x, y);
    }
    else {
        rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x));
    }
}
            

Was this page useful?