diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-06-14 13:53:31 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-06-14 13:53:31 -0700 |
commit | 78234fe605695d6e34431f8b491511e8dc963cfc (patch) | |
tree | bd0a7ab72181611deb38de5c7845a176e61a23f9 /activesupport | |
parent | f7114754b12f27a953a8e511128b394a4c2e85e5 (diff) | |
parent | 921ec9b5c6e92b5992de4bd898532be878ee5a36 (diff) | |
download | rails-78234fe605695d6e34431f8b491511e8dc963cfc.tar.gz rails-78234fe605695d6e34431f8b491511e8dc963cfc.tar.bz2 rails-78234fe605695d6e34431f8b491511e8dc963cfc.zip |
Merge pull request #10740 from mrsimo/hash-with-indifferent-access-select
HashWithIndifferentAccess#select working as intended
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 30 |
2 files changed, 34 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 0a81a8393d..56213de50e 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -227,6 +227,10 @@ module ActiveSupport def deep_symbolize_keys; to_hash.deep_symbolize_keys end def to_options!; self end + def select(*args, &block) + dup.select!(*args, &block) + end + # Convert to a regular hash with string keys. def to_hash _new_hash= {} diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 39bd0a2dd4..c6d7ab618c 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -480,6 +480,36 @@ class HashExtTest < ActiveSupport::TestCase assert_equal hash.delete('a'), nil end + def test_indifferent_select + hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).select {|k,v| v == 1} + + assert_equal({ 'a' => 1 }, hash) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash + end + + def test_indifferent_select_bang + indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings) + indifferent_strings.select! {|k,v| v == 1} + + assert_equal({ 'a' => 1 }, indifferent_strings) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings + end + + def test_indifferent_reject + hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).reject {|k,v| v != 1} + + assert_equal({ 'a' => 1 }, hash) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash + end + + def test_indifferent_reject_bang + indifferent_strings = ActiveSupport::HashWithIndifferentAccess.new(@strings) + indifferent_strings.reject! {|k,v| v != 1} + + assert_equal({ 'a' => 1 }, indifferent_strings) + assert_instance_of ActiveSupport::HashWithIndifferentAccess, indifferent_strings + end + def test_indifferent_to_hash # Should convert to a Hash with String keys. assert_equal @strings, @mixed.with_indifferent_access.to_hash |