Method: Object#<=>

Defined in:
object.c

#<=>(other) ⇒ 0?

Returns 0 if obj and other are the same object or obj == other, otherwise nil.

The #<=> is used by various methods to compare objects, for example Enumerable#sort, Enumerable#max etc.

Your implementation of #<=> should return one of the following values: -1, 0, 1 or nil. -1 means self is smaller than other. 0 means self is equal to other. 1 means self is bigger than other. Nil means the two values could not be compared.

When you define #<=>, you can include Comparable to gain the methods #<=, #<, #==, #>=, #> and #between?.

Returns:

  • (0, nil)


1703
1704
1705
1706
1707
1708
1709
# File 'object.c', line 1703

static VALUE
rb_obj_cmp(VALUE obj1, VALUE obj2)
{
    if (rb_equal(obj1, obj2))
        return INT2FIX(0);
    return Qnil;
}