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

2020-03-01 07:26发布

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

标签: ruby hash key
2条回答
唯我独甜
2楼-- · 2020-03-01 07:35

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

查看更多
相关推荐>>
3楼-- · 2020-03-01 07:48

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.

查看更多
登录 后发表回答