Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Hash#rehash
() -> self
rehash → self
hash.c
View on GitHub ↗
VALUE
rb_hash_rehash(VALUE hash)
{
VALUE tmp;
st_table *tbl;
if (hash_iterating_p(hash)) {
rb_raise(rb_eRuntimeError, "rehash during iteration");
}
rb_hash_modify_check(hash);
if (RHASH_AR_TABLE_P(hash)) {
tmp = hash_alloc_capa(0, RHASH_SIZE(hash));
if (RHASH_IDENTHASH_P(hash)) {
FL_SET_RAW(tmp, RHASH_COMPARE_BY_IDENTITY);
}
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
hash_ar_free_and_clear_table(hash);
ar_copy(hash, tmp);
}
else if (RHASH_ST_TABLE_P(hash)) {
st_table *old_tab = RHASH_ST_TABLE(hash);
tmp = hash_alloc_capa(0, 0);
if (old_tab->type == &identhash) {
FL_SET_RAW(tmp, RHASH_COMPARE_BY_IDENTITY);
}
hash_st_table_init(tmp, old_tab->num_entries);
RHASH_ST_TABLE(tmp)->type = old_tab->type;
tbl = RHASH_ST_TABLE(tmp);
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
hash_st_free(hash);
rb_hash_st_table_set(hash, tbl);
RHASH_ST_CLEAR(tmp);
}
hash_verify(hash);
return hash;
}
Rebuilds the hash table for self by recomputing the hash index for each key; returns self. Calling this method ensures that the hash table is valid.
The hash table becomes invalid if the hash value of a key has changed after the entry was created. See Modifying an Active Hash Key.