Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Array#|
[T] (::array[T] other_array) -> ::Array[Elem | T]
self | other_array → new_array
array.c
View on GitHub ↗
static VALUE
rb_ary_or(VALUE ary1, VALUE ary2)
{
ary2 = to_ary(ary2);
if (RARRAY_LEN(ary1) + RARRAY_LEN(ary2) <= SMALL_ARRAY_LEN) {
VALUE ary3 = rb_ary_new();
rb_ary_union(ary3, ary1);
rb_ary_union(ary3, ary2);
return ary3;
}
VALUE set = rb_obj_hide(rb_set_new_capa(RARRAY_LEN(ary1) + RARRAY_LEN(ary2)));
rb_ary_union_set(set, ary1);
rb_ary_union_set(set, ary2);
return rb_set_to_a(set);
}
Returns the union of self and other_array; duplicates are removed; order is preserved; items are compared using eql? and hash:
Example
[0, 1] | [2, 3] # => [0, 1, 2, 3] [0, 1, 1] | [2, 2, 3] # => [0, 1, 2, 3] [0, 1, 2] | [3, 2, 1, 0] # => [0, 1, 2, 3]
Related: see Methods for Combining.