As I mentioned before, one of the nice things about Ruby is its malleability. Here are a couple extensions to the Ruby Hash object:
class ::Hash def random return self.keys[rand(self.size)] end def byValue(subKey) if subKey self.sort_by {|key, value| value[subKey]}.reverse else self.sort_by {|key, value| value}.reverse end end end
Simply, Hash.random will now return a random hash key. Hash.byValue will sort the hash by value. Hash.byValue(‘priority’) will sort a Hash-of-Hashes by the value of the named sub-key. TIMTOWTDI, for sure.