Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Set in Ruby always preserve insertion order?

Tags:

ruby

set

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

like image 451
powerboy Avatar asked Apr 28 '12 06:04

powerboy


1 Answers

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

like image 83
Nevir Avatar answered Oct 06 '22 04:10

Nevir