aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew White <andrew.white@unboxed.co>2016-11-02 11:40:46 +0000
committerAndrew White <andrew.white@unboxed.co>2016-11-02 11:40:46 +0000
commitf762cf05a55b827684b756f6affdcd54923003c0 (patch)
tree03b411a9009ab3be6d501c64fa705ffc49a41c84
parentabe70e8ba18979a4519849f5276a82260f2a4cc4 (diff)
downloadrails-f762cf05a55b827684b756f6affdcd54923003c0.tar.gz
rails-f762cf05a55b827684b756f6affdcd54923003c0.tar.bz2
rails-f762cf05a55b827684b756f6affdcd54923003c0.zip
Fix AS::OrderHash#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/ordered_hash.rb14
-rw-r--r--activesupport/test/ordered_hash_test.rb4
2 files changed, 18 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index b0d4f2bd86..e81bd013b6 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -52,6 +52,20 @@ module ActiveSupport
true
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
+
# Hash is ordered in Ruby 1.9!
if RUBY_VERSION < '1.9'
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index a7fd9402c8..bbaccf2594 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -125,6 +125,9 @@ class OrderedHashTest < Test::Unit::TestCase
def test_select
assert_equal @keys, @ordered_hash.select { true }.map(&:first)
+ new_ordered_hash = @ordered_hash.select { true }
+ assert_equal @keys, new_ordered_hash.map(&:first)
+ assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
end
def test_delete_if
@@ -147,6 +150,7 @@ class OrderedHashTest < Test::Unit::TestCase
assert_equal copy, @ordered_hash
assert !new_ordered_hash.keys.include?('pink')
assert @ordered_hash.keys.include?('pink')
+ assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
end
def test_clear