|(p1)
public
Set Union — Returns a new array by joining ary with
other_ary, excluding any duplicates and preserving the order from
the given arrays.
It compares elements using their #hash and
#eql? methods for efficiency.
[ "a", "b", "c" ] | [ "c", "d", "a" ]
[ "c", "d", "a" ] | [ "a", "b", "c" ]
See also Array#uniq.
Show source
static VALUE
rb_ary_or(VALUE ary1, VALUE ary2)
{
VALUE hash, ary3;
long i;
ary2 = to_ary(ary2);
if (RARRAY_LEN(ary1) + RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) {
ary3 = rb_ary_new();
for (i=0; i<RARRAY_LEN(ary1); i++) {
VALUE elt = rb_ary_elt(ary1, i);
if (rb_ary_includes_by_eql(ary3, elt)) continue;
rb_ary_push(ary3, elt);
}
for (i=0; i<RARRAY_LEN(ary2); i++) {
VALUE elt = rb_ary_elt(ary2, i);
if (rb_ary_includes_by_eql(ary3, elt)) continue;
rb_ary_push(ary3, elt);
}
return ary3;
}
hash = ary_make_hash(ary1);
for (i=0; i<RARRAY_LEN(ary2); i++) {
VALUE elt = RARRAY_AREF(ary2, i);
if (!st_update(RHASH_TBL_RAW(hash), (st_data_t)elt, ary_hash_orset, (st_data_t)elt)) {
RB_OBJ_WRITTEN(hash, Qundef, elt);
}
}
ary3 = rb_hash_values(hash);
ary_recycle_hash(hash);
return ary3;
}