Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Array#intersection

(*::array[untyped] other_arrays) -> ::Array[Elem]
intersection(*other_arrays) → new_array

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.

See also #intersect? #join #inspect #keep_if also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0