Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

key ordering is something to rely on now, in Ruby as well as in JavaScript and Python

That ship has sailed - it was partly for the convenience of debugging, but the general purpose dict is now ordered.

It's only by insertion order - it's expensive to change the order - but the order can by relied upon and is being used.

Edit: some detail on changing the order - you can either remove all the key/value pairs up to the first one you want to change and add them back, or you can create a new dict with the pairs in the desired order. With Rust you can choose whether or not to have insertion order preserved https://stackoverflow.com/questions/42723065/how-to-sort-has...



Ya, my preference still stands whether the ship is docked or not. As you point out: it's good for debugging. As I mentioned, I like it for quick things in throwaway scripts. I don't like it in production code because when I'm reading something quickly that I don't care to deeply understand: Hashes are records and arrays order ordered lists. I very much like the way Rust does it (and I like a lot of what I've seen in Rust)--if we had `OrderedHash` in Ruby, I will feel completely different.


That seems arbitrary. I would consider that "politics" and like a place that had a rule to treat hashes as unordered a bit less.


I already implied it was politics by saying that it is something that needs to be determined at the team/org level.

I don't feel it's arbitrary, though, and to get a little clever about it I feel that ordered hashes are arbitrary and that's why I don't like to rely on it in _production_ code.

"Records" (aka hashes, aks dicts, aka maps, etc) are inherently unordered. Relying on insertion order or keyed values is super brittle.

So again, the fact that they are ordered is super convenient! It shines in one-off, temporary situations like debugging or writing a quick script to group something by a specific value without having to do any further sorting. But I don't believe it should ever be replied on in a production system, just for the fact that if the order ever changes, there are possibly many places that have to change.


I get that it's deterministic, but I can't really think of when I would use that for solving real programming tasks?

If it was sorted by a criteria of my choosing, sure. But I don't know when I last cared about insertion order...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: