Skip to content

Commit

Permalink
Fix OrderedHash.select to return self instance.
Browse files Browse the repository at this point in the history
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
arthurnn committed Mar 7, 2014
1 parent 6b927b8 commit b57da83
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
Expand Up @@ -28,6 +28,10 @@ def encode_with(coder)
coder.represent_seq '!omap', map { |k,v| { k => v } } coder.represent_seq '!omap', map { |k,v| { k => v } }
end end


def select(*args, &block)
dup.tap { |hash| hash.select!(*args, &block) }
end

def reject(*args, &block) def reject(*args, &block)
dup.tap { |hash| hash.reject!(*args, &block)} dup.tap { |hash| hash.reject!(*args, &block)}
end end
Expand Down
5 changes: 4 additions & 1 deletion activesupport/test/ordered_hash_test.rb
Expand Up @@ -120,7 +120,9 @@ def test_find_all
end end


def test_select 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 end


def test_delete_if def test_delete_if
Expand All @@ -143,6 +145,7 @@ def test_reject
assert_equal copy, @ordered_hash assert_equal copy, @ordered_hash
assert !new_ordered_hash.keys.include?('pink') assert !new_ordered_hash.keys.include?('pink')
assert @ordered_hash.keys.include?('pink') assert @ordered_hash.keys.include?('pink')
assert_instance_of ActiveSupport::OrderedHash, new_ordered_hash
end end


def test_clear def test_clear
Expand Down

0 comments on commit b57da83

Please sign in to comment.