aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew White <andrew.white@unboxed.co>2016-11-02 12:06:51 +0000
committerAndrew White <andrew.white@unboxed.co>2016-11-02 16:15:00 +0000
commit03e3aed60264ae0d12dfdf7a0c57044da49af148 (patch)
tree033860ff40461beae42a76a5f50ed601c851072f
parent21186270288ef3f8816cecf0aad1e1c537493bee (diff)
downloadrails-03e3aed60264ae0d12dfdf7a0c57044da49af148.tar.gz
rails-03e3aed60264ae0d12dfdf7a0c57044da49af148.tar.bz2
rails-03e3aed60264ae0d12dfdf7a0c57044da49af148.zip
Fix AS::HWIA#select and #reject on Ruby 2.1.1+
In Ruby 2.1.1 and later select and reject return a new instance of Hash rather than the subclass so we need to override them to return an instance of the correct class.
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb14
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb30
2 files changed, 44 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 9dc93de5a9..f89704d906 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -153,6 +153,20 @@ module ActiveSupport
def symbolize_keys; to_hash.symbolize_keys end
def to_options!; self end
+ if RUBY_VERSION > '2.1.0'
+ # On Ruby 2.1.1 and later the behavior of .select and reject changed to
+ # return a new Hash instance so we need to override them to return an
+ # instance of the correct class.
+
+ def select(*args, &block)
+ dup.tap { |hash| hash.select!(*args, &block) }
+ end
+
+ def reject(*args, &block)
+ dup.tap { |hash| hash.reject!(*args, &block) }
+ end
+ end
+
# Convert to a Hash with String keys.
def to_hash
Hash.new(default).merge!(self)
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index c3a59540fb..8c032dc880 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -259,6 +259,36 @@ class HashExtTest < Test::Unit::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