From af6552ed3c1035b5ccca78b5e6b414af853a36f6 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 15 Apr 2011 16:19:00 -0400 Subject: [PATCH] RUBY-265 semantics should == Hash#reject! --- lib/bson/ordered_hash.rb | 13 ++++++++++--- test/bson/ordered_hash_test.rb | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/bson/ordered_hash.rb b/lib/bson/ordered_hash.rb index 425942a..7c791a0 100644 --- a/lib/bson/ordered_hash.rb +++ b/lib/bson/ordered_hash.rb @@ -125,11 +125,11 @@ module BSON end def delete_if(&block) - self.each { |k,v| + self.each do |k,v| if yield k, v delete(k) end - } + end end def reject(&block) @@ -139,7 +139,14 @@ module BSON end def reject!(&block) - delete_if(&block) + changed = false + self.each do |k,v| + if yield k, v + changed = true + delete(k) + end + end + changed ? self : nil end def clear diff --git a/test/bson/ordered_hash_test.rb b/test/bson/ordered_hash_test.rb index ae97840..8a4f0d4 100644 --- a/test/bson/ordered_hash_test.rb +++ b/test/bson/ordered_hash_test.rb @@ -189,7 +189,8 @@ class OrderedHashTest < Test::Unit::TestCase def test_reject_bang @oh.reject! { |k, v| k == 'z' } - assert !@ok.keys.include?('z') + assert !@oh.keys.include?('z') + assert_nil @oh.reject! { |k, v| k == 'z' } end def test_clone