Method: Array#==

Defined in:
array.c

#==(other_array) ⇒ Boolean

Returns whether both:

  • self and other_array are the same size.

  • Their corresponding elements are the same; that is, for each index i in (0...self.size), self[i] == other_array[i].

Examples:

[:foo, 'bar', 2] == [:foo, 'bar', 2]   # => true
[:foo, 'bar', 2] == [:foo, 'bar', 2.0] # => true
[:foo, 'bar', 2] == [:foo, 'bar']      # => false # Different sizes.
[:foo, 'bar', 2] == [:foo, 'bar', 3]   # => false # Different elements.

This method is different from method Array#eql?, which compares elements using Object#eql?.

Related: see Methods for Comparing.

Returns:

  • (Boolean)


5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
# File 'array.c', line 5257

static VALUE
rb_ary_equal(VALUE ary1, VALUE ary2)
{
    if (ary1 == ary2) return Qtrue;
    if (!RB_TYPE_P(ary2, T_ARRAY)) {
        if (!rb_respond_to(ary2, idTo_ary)) {
            return Qfalse;
        }
        return rb_equal(ary2, ary1);
    }
    if (RARRAY_LEN(ary1) != RARRAY_LEN(ary2)) return Qfalse;
    if (RARRAY_CONST_PTR(ary1) == RARRAY_CONST_PTR(ary2)) return Qtrue;
    return rb_exec_recursive_paired(recursive_equal, ary1, ary2, ary2);
}