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 exclusive OR of self and the given enumerable; that is, containing each element that is in either self or enumerable, but not in both:

Example
set = Set[0, 1, 2]
set ^ Set[1, 2, 3]        # => Set[0, 3]
set ^ Set[2, 1]           # => Set[0]
set ^ Set[2, *('a'..'c')] # => Set[0, 1, "a", "b", "c"]
set ^ Set[2, 1, 0]        # => Set[]

For Set set and Enumerable enumerable, these expressions are equivalent:

Example
set ^ enumerable
((set | enumerable) - (set & enumerable))

Related: see Methods for Set Operations.

See also #>= #| #> #add also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0