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
set.c
View on GitHub ↗
static VALUE
set_i_xor(VALUE set, VALUE other)
{
VALUE new_set = rb_obj_dup(set);
if (rb_obj_is_kind_of(other, rb_cSet)) {
set_iter(other, set_xor_i, (st_data_t)new_set);
}
else {
VALUE tmp = set_s_alloc(rb_obj_class(new_set));
if (RSET_COMPARE_BY_IDENTITY(new_set)) {
set_i_compare_by_identity(tmp);
}
set_merge_enum_into(tmp, other);
set_iter(tmp, set_xor_i, (st_data_t)new_set);
}
set_compact_after_delete(set);
return 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.