Random Key From Ruby Hash
Join the DZone community and get the full member experience.Join For Free
Returns a random key from your hash, but won't repeat until it's done every one. (I was using this to help quiz myself.)
class Hash @keys_used =  def random_key @keys_used =  if @keys_used.size == self.size key = self.keys[rand(self.size)] while @keys_used.include?(key) key = self.keys[rand(self.size)] end @keys_used << key return key end end
Opinions expressed by DZone contributors are their own.