diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-02-16 21:57:14 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2018-02-16 21:58:30 -0500 |
commit | 971e22f43e16118db6ac3087c5ec75a740dbddca (patch) | |
tree | c90f3edff52553cbcd69f379c09529cde8316c96 /activesupport | |
parent | f7ebeeed23abb1296a2fc2db68df6e676f40617f (diff) | |
download | rails-971e22f43e16118db6ac3087c5ec75a740dbddca.tar.gz rails-971e22f43e16118db6ac3087c5ec75a740dbddca.tar.bz2 rails-971e22f43e16118db6ac3087c5ec75a740dbddca.zip |
Define transform_keys! in HashWithIndifferentAccess
Make sure that when transforming the keys of a HashWithIndifferentAccess
we can still access with indifferent access in Ruby 2.5.
Closes #32007.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 8 | ||||
-rw-r--r-- | activesupport/test/hash_with_indifferent_access_test.rb | 13 |
2 files changed, 21 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 2e2ed8a25d..eda7ec3940 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -311,6 +311,14 @@ module ActiveSupport dup.tap { |hash| hash.transform_keys!(*args, &block) } end + def transform_keys! + return enum_for(:transform_keys!) { size } unless block_given? + keys.each do |key| + self[yield(key)] = delete(key) + end + self + end + def slice(*keys) keys.map! { |key| convert_key(key) } self.class.new(super) diff --git a/activesupport/test/hash_with_indifferent_access_test.rb b/activesupport/test/hash_with_indifferent_access_test.rb index 41d653fa59..09f2598067 100644 --- a/activesupport/test/hash_with_indifferent_access_test.rb +++ b/activesupport/test/hash_with_indifferent_access_test.rb @@ -404,6 +404,12 @@ class HashWithIndifferentAccessTest < ActiveSupport::TestCase assert_equal({ "aa" => 1, "bb" => 2 }, hash) assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash + + hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).transform_keys { |k| k.to_sym } + + assert_equal(1, hash[:a]) + assert_equal(1, hash["a"]) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash end def test_indifferent_transform_keys_bang @@ -412,6 +418,13 @@ class HashWithIndifferentAccessTest < ActiveSupport::TestCase assert_equal({ "aa" => 1, "bb" => 2 }, indifferent_strings) assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings + + indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings) + indifferent_strings.transform_keys! { |k| k.to_sym } + + assert_equal(1, indifferent_strings[:a]) + assert_equal(1, indifferent_strings["a"]) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings end def test_indifferent_transform_values |