I\'m looking to extract n random key-value pairs from a hash.
If your sample has only one element, you could use this:
sample = h.keys.sample h.select { |k,v| k == sample }
Or if your sample contains more than one element, use this:
n = 2 sample = h.keys.sample(n) h.select { |k,v| sample.include?(k) }