Does Set in Ruby always preserve insertion order?

谁说我不能喝 提交于 2019-12-22 03:46:22

问题


i.e., is Ruby's Set equivalent to Java's LinkedHashSet?


回答1:


In Ruby 1.9: yes. In Ruby 1.8: probably not.

Set uses a Hash internally; and since hashes are insertion-ordered in 1.9, you're good to go!

As mu is too short points out, this is an implementation detail and could change in the future (though unlikely). Thankfully, the current implementation of Set is pure ruby, and could be adapted into an OrderedSet in the future if you like



来源:https://stackoverflow.com/questions/10361250/does-set-in-ruby-always-preserve-insertion-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!