Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Set#replace
(::_Each[A]) -> self
replace(enumerable) → self
set.c
View on GitHub ↗
static VALUE
set_i_replace(VALUE set, VALUE other)
{
rb_check_frozen(set);
if (rb_obj_is_kind_of(other, rb_cSet)) {
set_i_initialize_copy(set, other);
}
else {
if (set_iterating_p(set)) {
rb_raise(rb_eRuntimeError, "cannot replace set during iteration");
}
// make sure enum is enumerable before calling clear
enum_method_id(other);
set_table_clear(RSET_TABLE(set));
set_merge_enum_into(set, other);
}
set_compact_after_delete(set);
return set;
}
Replaces the contents self with the contents of the given enumerable; returns self:
Example
set = Set[1, 'c', :s] # => Set[1, "c", :s] set.replace([1, 2]) # => Set[1, 2]
Related: see Methods for Assigning.