Does Set in Ruby always preserve insertion order?

2019-04-21 19:50发布

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

标签: ruby set
1条回答
混吃等死
2楼-- · 2019-04-21 20:32

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

查看更多
登录 后发表回答