Class | Hash |
In: |
lib/hash.rb
|
Parent: | Object |
These are some extension functions for the Hash class
Pull out a random key out of a hash
# File lib/hash.rb, line 5 5: def random_key 6: keys.random 7: end
Pull a random key/value pair out of a hash
# File lib/hash.rb, line 15 15: def random_pair 16: key = random_key 17: {key => self[key]} 18: end
Pull between min and max random key/value pairs out of a hash
# File lib/hash.rb, line 21 21: def random_pairs min=1,max=3 22: max = size if max > size 23: min = 1 if min < 1 24: new_hash = {} 25: arr = Array.new(min+rand(max-min+1)) 26: arr.each{|x| new_hash.update(random_pair)} 27: new_hash 28: end