instance method
Hash#initialize_copy
(self object) -> self
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;
}
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}
Also replaces the default value or proc of self with the default value or proc of other_hash.
Example
h = {} other = Hash.new(:ok) h.replace(other) h.default # => :ok
Related: see Methods for Assigning.