diff options
author | Xavier Noria <fxn@hashref.com> | 2010-06-13 05:06:07 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-13 05:09:31 +0200 |
commit | 72f9fec607c4a72b2e88995d0cb32b16b96e9068 (patch) | |
tree | 6db9d499f1dd2251b17e04e9549d2c996e0d6cf8 | |
parent | 9183eaebed19f75980926fad2f3655f5f5e7826c (diff) | |
download | rails-72f9fec607c4a72b2e88995d0cb32b16b96e9068.tar.gz rails-72f9fec607c4a72b2e88995d0cb32b16b96e9068.tar.bz2 rails-72f9fec607c4a72b2e88995d0cb32b16b96e9068.zip |
hash merging with a block ignores non-existing keys altogether
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 2 | ||||
-rw-r--r-- | activesupport/test/ordered_hash_test.rb | 33 |
2 files changed, 17 insertions, 18 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index eda33f827e..b721e6f70a 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -131,7 +131,7 @@ module ActiveSupport def merge!(other_hash) if block_given? - other_hash.each {|k,v| self[k] = yield(k, self[k], v) } + other_hash.each {|k,v| self[k] = yield(k, self[k], v) if key? k} else other_hash.each {|k,v| self[k] = v } end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index 56369585ec..e19484fb82 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -144,28 +144,27 @@ class OrderedHashTest < Test::Unit::TestCase end def test_merge_with_block - other_hash = ActiveSupport::OrderedHash.new - other_hash['white'] = 'ff' - other_hash['black'] = '00' - merged = @ordered_hash.merge(other_hash) do |key, old_value, new_value| - new_value * 3 + hash = ActiveSupport::OrderedHash.new + hash[:a] = 0 + hash[:b] = 0 + merged = hash.merge(:b => 1, :c => 2) do |key, old_value, new_value| + new_value + 1 end - assert_equal 'ffffff', merged['white'] - assert_equal '000000', merged['black'] - - assert_nil @ordered_hash['white'] - assert_nil @ordered_hash['black'] + assert_equal 0, merged[:a] + assert_equal 2, merged[:b] + assert !merged.key?(:c) end def test_merge_bang_with_block - other_hash = ActiveSupport::OrderedHash.new - other_hash['white'] = 'ff' - other_hash['black'] = '00' - @ordered_hash.merge!(other_hash) do |key, old_value, new_value| - new_value * 3 + hash = ActiveSupport::OrderedHash.new + hash[:a] = 0 + hash[:b] = 0 + hash.merge!(:a => 1, :c => 2) do |key, old_value, new_value| + new_value + 3 end - assert_equal 'ffffff', @ordered_hash['white'] - assert_equal '000000', @ordered_hash['black'] + assert_equal 4, hash[:a] + assert_equal 0, hash[:b] + assert !hash.key?(:c) end def test_shift |