Ruby: What is the order of keys/values returned by

2020-03-01 06:56发布

问题:

Is it the same order in which the items were added to the Hash ?

回答1:

The top of the Ruby 1.9.2 documentation for the Hash class declares:

Hashes enumerate their values in the order that the corresponding keys were inserted.

Cursory tests suggest that this does indeed apply to both Hash#keys and Hash#values, although the corresponding documentation for those methods doesn't seem to specify it.



回答2:

In Ruby 1.8, there's no guaranteed order for the elements in a hash.



标签: ruby hash key