34

If I iterate over a hash once, then do so again without modifying the contents, are the keys guaranteed to appear in the same order?

A quick test suggests as much:

> h = {'a' => 1, 'b' => 2, 'c' => 3}
> 100_000.times.map { h.to_s == h.to_s }.all?
=> true

Another question, if the above is allowed, can I iterate through it changing only values, without adding any new keys, and have the ordering of the keys be unchanged?

similar to this python question: Do dicts preserve iteration order if they are not modified?

Unlike the proposed duplicate I'm not interested in whether the elements have a fully specified order, only the restriction that two consecutive iterations without modification provide the same sequence.

Community
  • 1
  • 1
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • As both answers state, insertion order is maintained. This confuses people who think "ordered" means "sorted", which they're not unless we're allowing for an invisible time stamp inside the Hash class. – the Tin Man Jun 28 '13 at 00:44
  • 1
    Possible duplicate of [Ruby: What is the order of keys/values returned by Hash.keys and Hash.values methods?](http://stackoverflow.com/questions/5208512/ruby-what-is-the-order-of-keys-values-returned-by-hash-keys-and-hash-values-met) – Rafał Cieślak Nov 14 '15 at 17:31

1 Answers1

64

Prior to 1.9, behavior of enumerated hashes was not in the ruby specification and therefore was up to implementation -- basically, hash enumeration behavior/pattern was undefined by the language and implementations could really do whatever they want (random? sorted? insertion order? different method every time? anything goes!)

1.9+, hash enumeration is specified by the language to be in the order of insertion, so if you know your platform is 1.9+, you can rely on it.

RubySpec

Justin L.
  • 13,510
  • 5
  • 48
  • 83
  • 5
    ruby 2.2.0+ doesn't follow rubyspec anymore, see http://rubini.us/2014/12/31/matz-s-ruby-developers-don-t-use-rubyspec/ – johnlinvc Mar 20 '15 at 04:06
  • And rubyspec spawned back to life after more than a thousand commits http://eregon.github.io/rubyspec/2015/07/29/rubyspec-is-reborn.html – nurettin Aug 25 '16 at 12:08