Permalink
Browse files

Fix OrderedHash.select to return self instance.

On ruby 2.1.1 the behavior of .select and .reject has changed.
They will return a Hash new instance, so we need to override them to
keep the instance object class.
  • Loading branch information...
1 parent 6dd0f68 commit af39f5871094d8c241103182edebedbb26e31d4c @arthurnn arthurnn committed with zzak Feb 26, 2014
Showing with 8 additions and 1 deletion.
  1. +4 −0 activesupport/lib/active_support/ordered_hash.rb
  2. +4 −1 activesupport/test/ordered_hash_test.rb
View
4 activesupport/lib/active_support/ordered_hash.rb
@@ -28,6 +28,10 @@ def encode_with(coder)
coder.represent_seq '!omap', map { |k,v| { k => v } }
end
+ def select(*args, &block)
+ dup.tap { |hash| hash.select!(*args, &block) }
+ end
+
def reject(*args, &block)
dup.tap { |hash| hash.reject!(*args, &block) }
end
View
5 activesupport/test/ordered_hash_test.rb
@@ -120,7 +120,9 @@ def test_find_all
end
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
@@ -143,6 +145,7 @@ def test_reject
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

0 comments on commit af39f58

Please sign in to comment.