1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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.
This commit is contained in:
Marc Schütz 2013-07-06 11:59:30 +02:00
parent 01d4941d0c
commit 20c065594f
3 changed files with 12 additions and 1 deletions

View file

@ -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*

View file

@ -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.

View file

@ -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}