Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Integer#<=>
(::Integer | ::Rational) -> ::Integer
(untyped) -> ::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));
}
}
Compares self and other.
Returns:
-
-1, ifselfis less thanother. -
0, ifselfis equal toother. -
1, ifselfis greater thenother. -
nil, ifselfandotherare incomparable.
Examples:
Example
1 <=> 2 # => -1 1 <=> 1 # => 0 1 <=> 1.0 # => 0 1 <=> Rational(1, 1) # => 0 1 <=> Complex(1, 0) # => 0 1 <=> 0 # => 1 1 <=> 'foo' # => nil
Class Integer includes module Comparable, each of whose methods uses Integer#<=> for comparison.