Skip to content

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

instance method

Set#&

(::_Each[A]) -> self
self & enumerable → new_set

Returns a new set containing the intersection of self and enumerable; that is, containing all elements common to both, with no duplicates. Argument enumerable must be an Enumerable object:

Example
set = Set[*(0..6), *%w[ a b c]] # => Set[0, 1, 2, 3, 4, 5, 6, "a", "b", "c"]
set & ['c', 6, 8, 4]            # => Set["c", 6, 4]
set & [:foo, :bar]              # => Set[]  # No elements in common.

Related: see Methods for Set Operations.

See also .new #+ .[] #- also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0