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.
This commit is contained in:
Arthur Neves 2014-02-26 15:22:52 -05:00
parent f60b524919
commit a94966ea09
No known key found for this signature in database
GPG Key ID: 04A390FB1E433E17
2 changed files with 8 additions and 1 deletions

View File

@ -28,6 +28,10 @@ module ActiveSupport
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 File

@ -120,7 +120,9 @@ class OrderedHashTest < ActiveSupport::TestCase
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 @@ class OrderedHashTest < ActiveSupport::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