diff --git a/lib/mongo/util/ordered_hash.rb b/lib/mongo/util/ordered_hash.rb index 568d7e5..209f4e5 100644 --- a/lib/mongo/util/ordered_hash.rb +++ b/lib/mongo/util/ordered_hash.rb @@ -51,4 +51,10 @@ class OrderedHash < Hash super(other) end + def inspect + str = '{' + str << @ordered_keys.collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ") + str << '}' + end + end diff --git a/tests/test_ordered_hash.rb b/tests/test_ordered_hash.rb index 3b6b7d5..60ea830 100644 --- a/tests/test_ordered_hash.rb +++ b/tests/test_ordered_hash.rb @@ -79,4 +79,8 @@ class OrderedHashTest < Test::Unit::TestCase assert_equal ['crab', 'apple', 3, 'foo'], @oh.values end + def test_inspect_retains_order + assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect + end + end