diff options
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 25 | ||||
-rw-r--r-- | activesupport/test/ordered_hash_test.rb | 22 |
2 files changed, 46 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 1ed7737017..fa68db5604 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -25,6 +25,25 @@ module ActiveSupport super end + def delete_if + super + sync_keys! + self + end + + def reject! + super + sync_keys! + self + end + + def reject(&block) + dup.reject!(&block) + end + + alias_method :super_keys, :keys + private :super_keys + def keys @keys end @@ -48,6 +67,12 @@ module ActiveSupport def each keys.each {|key| yield [key, self[key]]} end + + private + + def sync_keys! + (@keys - super_keys).each { |k| @keys.delete(k) } + end end end end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index 094f9316d6..ca5fce6267 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -83,4 +83,24 @@ class OrderedHashTest < Test::Unit::TestCase def test_each_with_index @ordered_hash.each_with_index { |pair, index| assert_equal [@keys[index], @values[index]], pair} end -end + + def test_delete_if + (copy = @ordered_hash.dup).delete('pink') + assert_equal copy, @ordered_hash.delete_if { |k, _| k == 'pink' } + assert !@ordered_hash.keys.include?('pink') + end + + def test_reject! + (copy = @ordered_hash.dup).delete('pink') + @ordered_hash.reject! { |k, _| k == 'pink' } + assert_equal copy, @ordered_hash + assert !@ordered_hash.keys.include?('pink') + end + + def test_reject + copy = @ordered_hash.dup + new_ordered_hash = @ordered_hash.reject { |k, _| k == 'pink' } + assert_equal copy, @ordered_hash + assert !new_ordered_hash.keys.include?('pink') + end +end
\ No newline at end of file |