Merge pull request #1572 from iguchi1124/support-ruby26-merge

Support ruby 2.6 Hash#merge with multiple arguments
This commit is contained in:
Kunpei Sakai 2020-01-02 04:44:44 +09:00
parent cf1c6b156b
commit 825299319e
No known key found for this signature in database
GPG Key ID: C4B6919318C291BC
2 changed files with 27 additions and 9 deletions

View File

@ -132,13 +132,17 @@ module Sinatra
super(*keys)
end
def merge!(other_hash)
return super if other_hash.is_a?(self.class)
other_hash.each_pair do |key, value|
key = convert_key(key)
value = yield(key, self[key], value) if block_given? && key?(key)
self[key] = convert_value(value)
def merge!(*other_hashes)
other_hashes.each do |other_hash|
if other_hash.is_a?(self.class)
super(other_hash)
else
other_hash.each_pair do |key, value|
key = convert_key(key)
value = yield(key, self[key], value) if block_given? && key?(key)
self[key] = convert_value(value)
end
end
end
self
@ -146,8 +150,8 @@ module Sinatra
alias_method :update, :merge!
def merge(other_hash, &block)
dup.merge!(other_hash, &block)
def merge(*other_hashes, &block)
dup.merge!(*other_hashes, &block)
end
def replace(other_hash)

View File

@ -205,6 +205,20 @@ class TestIndifferentHash < Minitest::Test
assert_equal 2, hash2[?q]
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
@hash.replace(?a=>1, :q=>2)
assert_equal({ ?a=>1, ?q=>2 }, @hash)