Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Array#intersection
(*::Array[untyped] | ::_ToAry[untyped] other_ary) -> ::Array[Elem]
intersection(*other_arrays) → new_array
array.c
View on GitHub ↗
static VALUE
rb_ary_intersection_multi(int argc, VALUE *argv, VALUE ary)
{
VALUE result = rb_ary_dup(ary);
int i;
for (i = 0; i < argc; i++) {
result = rb_ary_and(result, argv[i]);
}
return result;
}
Returns a new array containing each element in self that is eql? to at least one element in each of the given other_arrays; duplicates are omitted:
Example
[0, 0, 1, 1, 2, 3].intersection([0, 1, 2], [0, 1, 3]) # => [0, 1]
Each element must correctly implement method hash.
Order from self is preserved:
Example
[0, 1, 2].intersection([2, 1, 0]) # => [0, 1, 2]
Returns a copy of self if no arguments are given.
Related: see Methods for Combining.