In Ruby 1.9 a Hash is sorted on the basis of order of insertion.
Why the Ruby koans’s assertion on test_hash_is_unordered method returns true?
To me, the method’s title is quite misleading… maybe it refers to the fact that Ruby will recognize 2 equal hashes that were created with different keys order insetions.
But, theorically, this kind of assertion:
hash1 = { :one => "uno", :two => "dos" }
hash2 = { :two => "dos", :one => "uno" }
assert_equal ___, hash1 == hash2
Should return false. Or not?
From the fine manual:
So two Hashes are considered equal if they have the same key/value pairs regardless of order.
The examples in the documentation even contain this:
Yes, the
test_hash_is_unorderedtitle is somewhat misleading as order isn’t specifically being testing, only order with respect to equality is being demonstrated.