Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#&

array & other_array → new_array

Returns a new Array containing each element found in both array and Array other_array; duplicates are omitted; items are compared using eql?:

Example
[0, 1, 2, 3] & [1, 2] # => [1, 2]
[0, 1, 0, 1] & [0, 1] # => [0, 1]

Preserves order from array:

Example
[0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]

Related: Array#intersection.

See also .try_convert #* .new #+ also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1