Permalink
Browse files

Copy default_proc by Hash#dup.

  • Loading branch information...
shugo committed Dec 7, 2016
1 parent 2b0dd1f commit 338e0ff52d645556e644ac08b3efc6ea31868c59
Showing with 17 additions and 1 deletion.
  1. +13 −1 src/hash.c
  2. +4 −0 test/t/hash.rb
View
@@ -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);
@@ -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
View
@@ -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

0 comments on commit 338e0ff

Please sign in to comment.