Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Replaces the entire contents of self with the contents of other_hash; returns self:
h = {foo: 0, bar: 1, baz: 2}
h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
 
               static VALUE
rb_hash_replace(VALUE hash, VALUE hash2)
{
    rb_hash_modify_check(hash);
    if (hash == hash2) return hash;
    if (RHASH_ITER_LEV(hash) > 0) {
        rb_raise(rb_eRuntimeError, "can't replace hash during iteration");
    }
    hash2 = to_hash(hash2);

    COPY_DEFAULT(hash, hash2);

    if (RHASH_AR_TABLE_P(hash)) {
        ar_free_and_clear_table(hash);
    }
    else {
        st_free_table(RHASH_ST_TABLE(hash));
        RHASH_ST_CLEAR(hash);
    }
    hash_copy(hash, hash2);
    if (RHASH_EMPTY_P(hash2) && RHASH_ST_TABLE_P(hash2)) {
        /* ident hash */
        RHASH_ST_TABLE_SET(hash, st_init_table_with_size(RHASH_TYPE(hash2), 0));
    }

    rb_gc_writebarrier_remember(hash);

    return hash;
}
            

Was this page useful?