Hash#replace should copy default as well; close #2004

parent 9f6d69ee
......@@ -152,6 +152,11 @@ class Hash
hash.each_key{|k|
self[k] = hash[k]
}
if hash.default_proc
self.default_proc = hash.default_proc
elsif hash.default
self.default = hash.default
end
self
end
# ISO 15.2.13.4.17
......
......@@ -376,14 +376,12 @@ mrb_hash_aget(mrb_state *mrb, mrb_value self)
static mrb_value
mrb_hash_default(mrb_state *mrb, mrb_value hash)
{
mrb_value *argv;
int argc;
mrb_value key;
mrb_bool given;
mrb_get_args(mrb, "*", &argv, &argc);
mrb_get_args(mrb, "|o?", &key, &given);
if (MRB_RHASH_PROCDEFAULT_P(hash)) {
if (argc == 0) return mrb_nil_value();
key = argv[0];
if (!given) return mrb_nil_value();
return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, key);
}
else {
......
......@@ -230,6 +230,15 @@ assert('Hash#replace', '15.2.13.4.23') do
b = Hash.new.replace(a)
assert_equal({ 'abc_key' => 'abc_value' }, b)
a = Hash.new(42)
b = {}
b.replace(a)
assert_equal(42, b[1])
a = Hash.new{|h,x| x}
b.replace(a)
assert_equal(127, b[127])
end
assert('Hash#shift', '15.2.13.4.24') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment