mruby-hash-ext/hash.rb (merge!): takes multiple arguments.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-06-18 08:34:18 +09:00
parent 0565bbf85a
commit 909e1044ed
2 changed files with 18 additions and 10 deletions
+15 -10
View File
@@ -62,8 +62,8 @@ class Hash
##
# call-seq:
# hsh.merge!(other_hash) -> hsh
# hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
# hsh.merge!(other_hash..) -> hsh
# hsh.merge!(other_hash..){|key, oldval, newval| block} -> hsh
#
# Adds the contents of _other_hash_ to _hsh_. If no block is specified,
# entries with duplicate keys are overwritten with the values from
@@ -81,14 +81,19 @@ class Hash
# #=> {"a"=>100, "b"=>200, "c"=>300}
#
def merge!(other, &block)
raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
if block
other.each_key{|k|
self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
}
else
other.each_key{|k| self[k] = other[k]}
def merge!(*others, &block)
i = 0; len=others.size
while i<len
other = others[i]
i += 1
raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
if block
other.each_key{|k|
self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
}
else
other.each_key{|k| self[k] = other[k]}
end
end
self
end
+3
View File
@@ -64,6 +64,9 @@ assert('Hash#merge!') do
assert_raise(TypeError) do
{ 'abc_key' => 'abc_value' }.merge! "a"
end
# multiple arguments
assert_equal({a:1,b:2,c:3}, {a:1}.merge!({b:2},{c:3}))
end
assert('Hash#values_at') do