Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Array#difference

(*::Array[untyped] arrays) -> ::Array[Elem]
difference(*other_arrays = []) → new_array

Returns a new array containing only those elements from self that are not found in any of the given other_arrays; items are compared using eql?; order from self is preserved:

Example
[0, 1, 1, 2, 1, 1, 3, 1, 1].difference([1]) # => [0, 2, 3]
[0, 1, 2, 3].difference([3, 0], [1, 3])     # => [2]
[0, 1, 2].difference([4])                   # => [0, 1, 2]
[0, 1, 2].difference                        # => [0, 1, 2]

Returns a copy of self if no arguments are given.

Related: Array#-; see also Methods for Combining.

See also #delete_if #dig #delete_at #drop also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0