Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Hash#replace
hash.replace(other_hash) → self
hash.c
View on GitHub ↗
static VALUE
rb_hash_replace(VALUE hash, VALUE hash2)
{
rb_hash_modify_check(hash);
if (hash == hash2) return hash;
if (hash_iterating_p(hash)) {
rb_raise(rb_eRuntimeError, "can't replace hash during iteration");
}
hash2 = to_hash(hash2);
COPY_DEFAULT(hash, hash2);
if (RHASH_AR_TABLE_P(hash)) {
hash_ar_free_and_clear_table(hash);
}
else {
hash_st_free_and_clear_table(hash);
}
hash_copy(hash, hash2);
return hash;
}
Alias of #initialize_copy
Replaces the entire contents of self with the contents of other_hash; returns self:
Example
h = {foo: 0, bar: 1, baz: 2} h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}