Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 20c065594f1434c93e6fe64cad062a0df1f42a8e 1 parent 01d4941
Marc Schütz schuetzm authored
5 activesupport/CHANGELOG.md
View
@@ -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*
2  activesupport/lib/active_support/hash_with_indifferent_access.rb
View
@@ -228,7 +228,7 @@ def deep_symbolize_keys; to_hash.deep_symbolize_keys! end
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.
6 activesupport/test/core_ext/hash_ext_test.rb
View
@@ -487,6 +487,12 @@ def test_indifferent_select
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}
Please sign in to comment.
Something went wrong with that request. Please try again.