Skip to content

Commit

Permalink
Copy default_proc by Hash#dup.
Browse files Browse the repository at this point in the history
  • Loading branch information
shugo committed Dec 7, 2016
1 parent 2b0dd1f commit 338e0ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mrb_hash_dup(mrb_state *mrb, mrb_value hash)
struct RHash* ret;
khash_t(ht) *h, *ret_h;
khiter_t k, ret_k;
mrb_value ifnone, vret;

h = RHASH_TBL(hash);
ret = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class);
Expand All @@ -243,7 +244,18 @@ mrb_hash_dup(mrb_state *mrb, mrb_value hash)
}
}

return mrb_obj_value(ret);
if (MRB_RHASH_DEFAULT_P(hash)) {
ret->flags |= MRB_HASH_DEFAULT;
}
if (MRB_RHASH_PROCDEFAULT_P(hash)) {
ret->flags |= MRB_HASH_PROC_DEFAULT;
}
vret = mrb_obj_value(ret);
ifnone = RHASH_IFNONE(hash);
if (!mrb_nil_p(ifnone)) {
mrb_iv_set(mrb, vret, mrb_intern_lit(mrb, "ifnone"), ifnone);
}
return vret;
}

MRB_API mrb_value
Expand Down
4 changes: 4 additions & 0 deletions test/t/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def hash.default(k); self[k] = 1; end
b = a.dup
a['a'] = 2
assert_equal({'a' => 1}, b)

c = Hash.new { |h, k| h[k] = k.upcase }
d = c.dup
assert_equal("FOO", d["foo"])
end

assert('Hash#default', '15.2.13.4.5') do
Expand Down

0 comments on commit 338e0ff

Please sign in to comment.