In: |
rand.rb
|
Parent: | Object |
Choose and return a random key-value pair of self.
{:one => 1, :two => 2, :three => 3}.pick #=> [:one, 1]
# File rand.rb, line 91 91: def pick 92: k = keys.pick 93: [k, self[k]] 94: end
Deletes a random key-value pair of self, returning that pair.
a = {:one => 1, :two => 2, :three => 3} a.pick #=> [:two, 2] a #=> {:one => 1, :three => 3}
# File rand.rb, line 100 100: def pick! 101: rv = pick 102: delete rv.first 103: rv 104: end
Return a random key of self.
{:one => 1, :two => 2, :three => 3}.pick_key #=> :three
# File rand.rb, line 108 108: def pick_key 109: keys.pick 110: end
Delete a random key-value pair of self and return the key.
a = {:one => 1, :two => 2, :three => 3} a.pick_key! #=> :two a #=> {:one => 1, :three => 3}
# File rand.rb, line 122 122: def pick_key! 123: pick!.first 124: end
Return a random value of self.
{:one => 1, :two => 2, :three => 3}.pick_value #=> 3
# File rand.rb, line 114 114: def pick_value 115: values.pick 116: end
Delete a random key-value pair of self and return the value.
a = {:one => 1, :two => 2, :three => 3} a.pick_value! #=> 2 a #=> {:one => 1, :three => 3}
# File rand.rb, line 130 130: def pick_value! 131: pick!.last 132: end
Return a copy of self with values arranged in random order.
{:one => 1, :two => 2, :three => 3}.shuffle_hash #=> {:two=>2, :three=>1, :one=>3}
# File rand.rb, line 145 145: def shuffle_hash 146: shuffled = {} 147: shuffle_hash_pairs.each{|k, v| 148: shuffled[k] = v 149: } 150: shuffled 151: end
Destructive shuffle_hash. Arrange the values of self in new, random order.
h = {:one => 1, :two => 2, :three => 3} h.shuffle_hash! h #=> {:two=>2, :three=>1, :one=>3}
# File rand.rb, line 158 158: def shuffle_hash! 159: shuffle_hash_pairs.each{|k, v| 160: self[k] = v 161: } 162: self 163: end