Added OrderedHash#inspect which preserves key order

This commit is contained in:
Jim Menard 2009-01-09 13:54:12 -05:00
parent 5b83544b9c
commit a9ba8c62fd
2 changed files with 10 additions and 0 deletions

View File

@ -51,4 +51,10 @@ class OrderedHash < Hash
super(other) super(other)
end end
def inspect
str = '{'
str << @ordered_keys.collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
str << '}'
end
end end

View File

@ -79,4 +79,8 @@ class OrderedHashTest < Test::Unit::TestCase
assert_equal ['crab', 'apple', 3, 'foo'], @oh.values assert_equal ['crab', 'apple', 3, 'foo'], @oh.values
end end
def test_inspect_retains_order
assert_equal '{"c"=>1, "a"=>2, "z"=>3}', @oh.inspect
end
end end