diff --git a/lib/bson/ordered_hash.rb b/lib/bson/ordered_hash.rb index af82ecc..c523dab 100644 --- a/lib/bson/ordered_hash.rb +++ b/lib/bson/ordered_hash.rb @@ -159,20 +159,22 @@ module BSON @ordered_keys = [] end - def hash - code = 17 - each_pair do |key, value| - code = 37 * code + key.hash - code = 37 * code + value.hash + if RUBY_VERSION =~ /1.8.6/ + def hash + code = 17 + each_pair do |key, value| + code = 37 * code + key.hash + code = 37 * code + value.hash + end + code & 0x7fffffff end - code & 0x7fffffff - end - def eql?(o) - if o.instance_of? BSON::OrderedHash - self.hash == o.hash - else - false + def eql?(o) + if o.instance_of? BSON::OrderedHash + self.hash == o.hash + else + false + end end end diff --git a/test/bson/ordered_hash_test.rb b/test/bson/ordered_hash_test.rb index c837eb3..16e05ea 100644 --- a/test/bson/ordered_hash_test.rb +++ b/test/bson/ordered_hash_test.rb @@ -45,6 +45,23 @@ class OrderedHashTest < Test::Unit::TestCase assert_equal 1, list.uniq.size end + if !(RUBY_VERSION =~ /1.8.6/) + def test_compatibility_with_hash + list = [] + doc = BSON::OrderedHash.new + doc['_id'] = 'ab12' + doc['name'] = 'test' + + doc2 = {} + doc2['_id'] = 'ab12' + doc2['name'] = 'test' + list << doc + list << doc2 + + assert_equal 1, list.uniq.size + end + end + def test_equality a = BSON::OrderedHash.new a['x'] = 1