From 36143d26cb841210b5f22aff4ed9c093a0554a1a Mon Sep 17 00:00:00 2001
From: Xavier Noria <fxn@hashref.com>
Date: Sun, 13 Jun 2010 06:37:54 +0200
Subject: revises implementation of AS::OrderedHash#merge!

---
 activesupport/CHANGELOG                          |  2 +-
 activesupport/lib/active_support/ordered_hash.rb | 10 ++++---
 activesupport/test/ordered_hash_test.rb          | 33 ++++++++++++------------
 3 files changed, 24 insertions(+), 21 deletions(-)

(limited to 'activesupport')

diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 54ac9d9588..13ec3c3775 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,6 +1,6 @@
 *Rails 3.0.0 [Release Candidate] (unreleased)*
 
-* Ruby 1.9 compatibility: ActiveSupport::OrderedHash#merge and #merge! accept a block. #4838 [Paul Mucur]
+* ActiveSupport::OrderedHash#merge and #merge! accept a block. #4838 [Paul Mucur, fxn]
 
 * Date#since, #ago, #beginning_of_day, #end_of_day, and #xmlschema honor now the user time zone if set. [Geoff Buesing]
 
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index eda33f827e..cd450fd00f 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -130,10 +130,12 @@ module ActiveSupport
       end
 
       def merge!(other_hash)
-        if block_given?
-          other_hash.each {|k,v| self[k] = yield(k, self[k], v) }
-        else
-          other_hash.each {|k,v| self[k] = v }
+        other_hash.each do |k, v|
+          if block_given? && key?(k)
+            self[k] = yield k, self[k], v
+          else
+            self[k] = v
+          end
         end
         self
       end
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index 56369585ec..0f36f5204d 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -144,28 +144,29 @@ 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 => 2, :c => 7) 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 3, merged[:b]
+    assert_equal 7, merged[: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 => 7) 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_equal 7, hash[:c]
   end
 
   def test_shift
-- 
cgit v1.2.3