End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Integer#<=>
self <=> other → -1, 0, +1, or nil
numeric.c
View on GitHub ↗
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));
}
}
Returns:
-
-1, if
selfis less thanother. -
0, if
selfis equal toother. -
1, if
selfis greater thenother. -
nil, ifselfandotherare incomparable.
Examples:
Example
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.