End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Array#<=>
array <=> other_array → -1, 0, or 1
array.c
View on GitHub ↗
VALUE
rb_ary_cmp(VALUE ary1, VALUE ary2)
{
long len;
VALUE v;
ary2 = rb_check_array_type(ary2);
if (NIL_P(ary2)) return Qnil;
if (ary1 == ary2) return INT2FIX(0);
v = rb_exec_recursive_paired(recursive_cmp, ary1, ary2, ary2);
if (v != Qundef) return v;
len = RARRAY_LEN(ary1) - RARRAY_LEN(ary2);
if (len == 0) return INT2FIX(0);
if (len > 0) return INT2FIX(1);
return INT2FIX(-1);
}
Returns -1, 0, or 1 as self is less than, equal to, or greater than other_array. For each index i in self, evaluates result = self[i] <=> other_array[i].
Returns -1 if any result is -1:
Example
[0, 1, 2] <=> [0, 1, 3] # => -1
Returns 1 if any result is 1:
Example
[0, 1, 2] <=> [0, 1, 1] # => 1
When all results are zero:
-
Returns -1 if
arrayis smaller thanother_array:Example[0, 1, 2] <=> [0, 1, 2, 3] # => -1
-
Returns 1 if
arrayis larger thanother_array:Example[0, 1, 2] <=> [0, 1] # => 1
-
Returns 0 if
arrayandother_arrayare the same size:Example[0, 1, 2] <=> [0, 1, 2] # => 0