mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-hash-ext/hash.rb (merge!): takes multiple arguments.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user