Fixed OrderedHash key deletion.

This commit is contained in:
Jim Menard 2009-01-21 10:53:26 -05:00
parent d3d0d5a16e
commit 5c751e9cfc
2 changed files with 35 additions and 0 deletions

View File

@ -63,6 +63,24 @@ class OrderedHash < Hash
str << '}'
end
def delete(key, &block)
@ordered_keys.delete(key)
super
end
def delete_if(&block)
self.each { |k,v|
if yield k, v
delete(k)
end
}
end
def clear
super
@ordered_keys = []
end
end # Ruby before 1.9
end

View File

@ -82,4 +82,21 @@ class OrderedHashTest < Test::Unit::TestCase
assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
end
def test_clear
@oh.clear
assert @oh.keys.empty?
end
def test_delete
assert @oh.keys.include?('z')
@oh.delete('z')
assert !@oh.keys.include?('z')
end
def test_delete_if
assert @oh.keys.include?('z')
@oh.delete_if { |k,v| k == 'z' }
assert !@oh.keys.include?('z')
end
end