diff options
author | Marc Schütz <schuetzm@gmx.net> | 2013-07-06 11:59:30 +0200 |
---|---|---|
committer | Marc Schütz <schuetzm@gmx.net> | 2013-07-06 15:56:07 +0200 |
commit | 20c065594f1434c93e6fe64cad062a0df1f42a8e (patch) | |
tree | d56af1342f4e79dde5a1f2328ad2b429d6465e62 /activesupport | |
parent | 01d4941d0c87764742562c59d4bf7bb34ac108c4 (diff) | |
download | rails-20c065594f1434c93e6fe64cad062a0df1f42a8e.tar.gz rails-20c065594f1434c93e6fe64cad062a0df1f42a8e.tar.bz2 rails-20c065594f1434c93e6fe64cad062a0df1f42a8e.zip |
Make HashWithIndifferentAccess#select always return the hash.
Hash#select! returns nil if the hash didn't change and thus behaves differently
from select, so it's return value can't be used as result for the latter.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/hash_with_indifferent_access.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 6 |
3 files changed, 12 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 8c07a034cc..221ceb532a 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Make `HashWithIndifferentAccess#select` always return the hash, even when + `Hash#select!` returns `nil`, to allow further chaining. + + *Marc Schütz* + * Remove deprecated `String#encoding_aware?` core extensions (`core_ext/string/encoding`). *Arun Agrawal* diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 95e03ba95c..3da99872c0 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -228,7 +228,7 @@ module ActiveSupport def to_options!; self end def select(*args, &block) - dup.select!(*args, &block) + dup.tap {|hash| hash.select!(*args, &block)} end # Convert to a regular hash with string keys. diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 0857796036..2d0c56bef5 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -487,6 +487,12 @@ class HashExtTest < ActiveSupport::TestCase assert_instance_of ActiveSupport::HashWithIndifferentAccess, hash end + def test_indifferent_select_returns_a_hash_when_unchanged + hash = ActiveSupport::HashWithIndifferentAccess.new(@strings).select {|k,v| true} + + 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} |