Merge pull request #1572 from iguchi1124/support-ruby26-merge
Support ruby 2.6 Hash#merge with multiple arguments
This commit is contained in:
parent
cf1c6b156b
commit
825299319e
|
@ -132,22 +132,26 @@ module Sinatra
|
||||||
super(*keys)
|
super(*keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge!(other_hash)
|
def merge!(*other_hashes)
|
||||||
return super if other_hash.is_a?(self.class)
|
other_hashes.each do |other_hash|
|
||||||
|
if other_hash.is_a?(self.class)
|
||||||
|
super(other_hash)
|
||||||
|
else
|
||||||
other_hash.each_pair do |key, value|
|
other_hash.each_pair do |key, value|
|
||||||
key = convert_key(key)
|
key = convert_key(key)
|
||||||
value = yield(key, self[key], value) if block_given? && key?(key)
|
value = yield(key, self[key], value) if block_given? && key?(key)
|
||||||
self[key] = convert_value(value)
|
self[key] = convert_value(value)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :update, :merge!
|
alias_method :update, :merge!
|
||||||
|
|
||||||
def merge(other_hash, &block)
|
def merge(*other_hashes, &block)
|
||||||
dup.merge!(other_hash, &block)
|
dup.merge!(*other_hashes, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def replace(other_hash)
|
def replace(other_hash)
|
||||||
|
|
|
@ -205,6 +205,20 @@ class TestIndifferentHash < Minitest::Test
|
||||||
assert_equal 2, hash2[?q]
|
assert_equal 2, hash2[?q]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_merge_with_multiple_argument
|
||||||
|
hash = Sinatra::IndifferentHash.new.merge({a: 1}, {b: 2}, {c: 3})
|
||||||
|
assert_equal 1, hash[?a]
|
||||||
|
assert_equal 2, hash[?b]
|
||||||
|
assert_equal 3, hash[?c]
|
||||||
|
|
||||||
|
hash2 = Sinatra::IndifferentHash[d: 4]
|
||||||
|
hash3 = {e: 5}
|
||||||
|
hash.merge!(hash2, hash3)
|
||||||
|
|
||||||
|
assert_equal 4, hash[?d]
|
||||||
|
assert_equal 5, hash[?e]
|
||||||
|
end
|
||||||
|
|
||||||
def test_replace
|
def test_replace
|
||||||
@hash.replace(?a=>1, :q=>2)
|
@hash.replace(?a=>1, :q=>2)
|
||||||
assert_equal({ ?a=>1, ?q=>2 }, @hash)
|
assert_equal({ ?a=>1, ?q=>2 }, @hash)
|
||||||
|
|
Loading…
Reference in New Issue