Ruby Hash .keys and .values, safe to assume same order?

hash, ruby

Solution

Yes. According to the Ruby Docs for Hash, "Hashes enumerate their values in the order that the corresponding keys were inserted." So you should always get the same order for a hash if it is created in the same way.

Problem

Rudimentary irb testing suggests that Ruby Hash returns `.keys` and `.values` in matching order. Is it safe to assume that this is the case?

Original source