diff options
author | Paul Mucur <mudge@mudge.name> | 2010-06-11 23:38:18 +0100 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-13 04:19:35 +0200 |
commit | 58e21a4a0d4eefc395139e88c1f184b9eaf0b4c4 (patch) | |
tree | b1af32b1a6f57366f6f916c4ffbf516f814483f5 /activesupport | |
parent | a087bc85fd24e7261ce6a6f63ffa2dbd49db567d (diff) | |
download | rails-58e21a4a0d4eefc395139e88c1f184b9eaf0b4c4.tar.gz rails-58e21a4a0d4eefc395139e88c1f184b9eaf0b4c4.tar.bz2 rails-58e21a4a0d4eefc395139e88c1f184b9eaf0b4c4.zip |
Support passing a block to ActiveSupport::OrderedHash's merge and merge! [#4838 state:committed]
For better consistency with Ruby's own Hash implementation.
Signed-off-by: Xavier Noria <fxn@hashref.com>
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 10 | ||||
-rw-r--r-- | activesupport/test/ordered_hash_test.rb | 13 |
3 files changed, 22 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 3f61e9949d..54ac9d9588 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0.0 [Release Candidate] (unreleased)* +* Ruby 1.9 compatibility: ActiveSupport::OrderedHash#merge and #merge! accept a block. #4838 [Paul Mucur] + * 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 e1a2866863..eda33f827e 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -130,12 +130,16 @@ module ActiveSupport end def merge!(other_hash) - other_hash.each {|k,v| self[k] = v } + if block_given? + other_hash.each {|k,v| self[k] = yield(k, self[k], v) } + else + other_hash.each {|k,v| self[k] = v } + end self end - def merge(other_hash) - dup.merge!(other_hash) + def merge(other_hash, &block) + dup.merge!(other_hash, &block) end # When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not. diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index d070206d44..66e727007a 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -142,9 +142,22 @@ class OrderedHashTest < Test::Unit::TestCase assert_equal merged.length, @ordered_hash.length + other_hash.length assert_equal @keys + ['purple', 'violet'], merged.keys + another_hash = ActiveSupport::OrderedHash.new + another_hash['white'] = 'ff' + another_hash['black'] = '00' + merged_with_block = @ordered_hash.merge(another_hash) do |key, old_value, new_value| + new_value * 3 + end + assert_equal 'ffffff', merged_with_block['white'] + assert_equal '000000', merged_with_block['black'] + @ordered_hash.merge! other_hash assert_equal @ordered_hash, merged assert_equal @ordered_hash.keys, merged.keys + + @ordered_hash.merge! another_hash + assert_equal 'ffffff', merged_with_block['white'] + assert_equal '000000', merged_with_block['black'] end def test_shift |