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.