4 ms·
Ruby 1.9 hashes preserve insertion order, so you could use them as arrays. Not sure why you'd want to, Ruby still has separate arrays.
by IsaacL 15y ago
Ruby 1.9 hashes preserve insertion order, so you could use them as arrays. Not sure why you'd want to, Ruby still has separate arrays.
- lambda_cube 15y agoThe point of preserving insertion order is not to be able to treat hashes as arrays. It's to get deterministic and repeatable iteration order between different runs. Java has LinkedHashMap and LinkedHashSet if you care about iteration order and HashMap and HashSet if you don't. With the repeatability it's easier to write automatic tests. I worked at a company making a compiler in Java and we got different binaries when compiling without changing the source code. After we changed to LinkedHashMap and LinkedHashSet that problem was gone. That's an example where we wanted the deterministic aspect of the Linked versions. Another way to get the same behavior in Java is to use TreeMap and TreeSet, but that only works if the things you put in are comparable, and you get different time complexities as well.