Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Set#reset
() -> self
reset → self
set.c
View on GitHub ↗
static VALUE
set_i_reset(VALUE set)
{
if (set_iterating_p(set)) {
rb_raise(rb_eRuntimeError, "reset during iteration");
}
return set_reset_table_with_type(set, RSET_TABLE(set)->type);
}
Resets the internal state of self; returns self.
A set relies on the hash results of each element being consistent. Modifying an element in a way that changes the results of hash may allow duplicate elements in the set:
Example
array = [1] set = Set[array] # => Set[[1]] array << 2 set.add(array) # => Set[[1, 2], [1, 2]]
Calling reset will recalculate all of the hash values and remove duplicate elements:
Example
set.reset # => Set[[1, 2]]