From d43b1b0a9eb5bc46818224d0939223ea31a41b00 Mon Sep 17 00:00:00 2001 From: claudiob Date: Tue, 23 Sep 2014 11:18:11 -0700 Subject: Add necessary 'require reverse_merge' to HAWI.rb Hashes with indifferent access should support `reverse_merge` out-of-the-box but they don't; for instance the following code fails: ```ruby require 'active_support' require 'active_support/hash_with_indifferent_access' hash = HashWithIndifferentAccess.new key: :old_value hash.reverse_merge key: :new_value ``` This PR fixes the case above by simply requiring `active_support/core_ext/hash/reverse_merge` in `hash_with_indifferent_access.rb` and adding a test that confirms the fix. --- Here are more details about the bugfix. Currently, `reverse_merge` is [defined in HashWithIndifferentAccess](https://github.com/rails/rails/blob/4e8ea13ba1a0870905a46fac5f232d9f41eef8a4/activesupport/lib/active_support/hash_with_indifferent_access.rb#L208) by invoking `super`, that is by invoking `Hash#reverse_merge`: ```ruby def reverse_merge(other_hash) super(self.class.new_from_hash_copying_default(other_hash)) end ``` However, Ruby's `Hash` does not have the `reverse_merge` by default: it must be added by ActiveSupport, and that requires the following line of code to be present: ```ruby require 'active_support/core_ext/hash/reverse_merge' ``` --- .../lib/active_support/hash_with_indifferent_access.rb | 1 + activesupport/test/hash_with_indifferent_access_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 activesupport/test/hash_with_indifferent_access_test.rb diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 3d8f2d572b..1468c62151 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/hash/keys' +require 'active_support/core_ext/hash/reverse_merge' module ActiveSupport # Implements a hash where keys :foo and "foo" are considered diff --git a/activesupport/test/hash_with_indifferent_access_test.rb b/activesupport/test/hash_with_indifferent_access_test.rb new file mode 100644 index 0000000000..843994147b --- /dev/null +++ b/activesupport/test/hash_with_indifferent_access_test.rb @@ -0,0 +1,10 @@ +require 'abstract_unit' +require 'active_support/hash_with_indifferent_access' + +class HashWithIndifferentAccessTest < ActiveSupport::TestCase + def test_reverse_merge + hash = HashWithIndifferentAccess.new key: :old_value + hash.reverse_merge! key: :new_value + assert_equal :old_value, hash[:key] + end +end \ No newline at end of file -- cgit v1.2.3