Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
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:
[0, 1, 2] <=> [0, 1, 3] # => -1
Returns 1 if any result is 1:
[0, 1, 2] <=> [0, 1, 1] # => 1
When all results are zero:
  • Returns -1 if array is smaller than other_array:
    [0, 1, 2] <=> [0, 1, 2, 3] # => -1
    
  • Returns 1 if array is larger than other_array:
    [0, 1, 2] <=> [0, 1] # => 1
    
  • Returns 0 if array and other_array are the same size:
    [0, 1, 2] <=> [0, 1, 2] # => 0
    
 
               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);
}
            

Was this page useful?