hash.rb (merge): now takes multiple arguments.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-06-18 08:10:25 +09:00
parent 7dec6e776e
commit 14e5c0c7b9
+13 -8
View File
@@ -158,15 +158,20 @@ class Hash
# will be the final value of this element.
#
# ISO 15.2.13.4.22
def merge(other, &block)
raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
def merge(*others, &block)
i=0; len=others.size
h = self.dup
if block
other.each_key{|k|
h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
}
else
other.each_key{|k| h[k] = other[k]}
while i<len
other = others[i]
i += 1
raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
if block
other.each_key{|k|
h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
}
else
other.each_key{|k| h[k] = other[k]}
end
end
h
end