Method: Hash#replace
- Defined in:
- hash.c
#replace(other_hash) ⇒ Hash
Replaces the contents of hsh with the contents of other_hash.
h = { "a" => 100, "b" => 200 }
h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 |
# File 'hash.c', line 1445
static VALUE
rb_hash_replace(VALUE hash, VALUE hash2)
{
st_table *table2;
rb_hash_modify_check(hash);
if (hash == hash2) return hash;
hash2 = to_hash(hash2);
RHASH_SET_IFNONE(hash, RHASH_IFNONE(hash2));
if (FL_TEST(hash2, HASH_PROC_DEFAULT))
FL_SET(hash, HASH_PROC_DEFAULT);
else
FL_UNSET(hash, HASH_PROC_DEFAULT);
table2 = RHASH(hash2)->ntbl;
rb_hash_clear(hash);
if (table2) hash_tbl(hash)->type = table2->type;
rb_hash_foreach(hash2, replace_i, hash);
return hash;
}
|