Random::Hash

Public Instance Methods

at_rand() click to toggle source
Alias for: rand_value
at_rand!() click to toggle source
Alias for: rand_value!
pick() click to toggle source
Alias for: rand_value!
pick_key() click to toggle source
Alias for: rand_key!
pick_pair() click to toggle source
Alias for: rand_pair!
rand_key() click to toggle source

Returns a random key.

  {:one => 1, :two => 2, :three => 3}.pick_key  #=> :three
     # File lib/more/facets/random.rb, line 258
258:     def rand_key
259:       keys.at(Random.number(keys.size))
260:     end
rand_key!() click to toggle source

Delete a random key-value pair, returning the key.

  a = {:one => 1, :two => 2, :three => 3}
  a.pick_key!  #=> :two
  a            #=> {:one => 1, :three => 3}
     # File lib/more/facets/random.rb, line 268
268:     def rand_key!
269:       k,v = rand_pair
270:       delete(k)
271:       return k
272:     end
Also aliased as: pick_key
rand_pair() click to toggle source

Returns a random key-value pair.

  {:one => 1, :two => 2, :three => 3}.pick  #=> [:one, 1]
     # File lib/more/facets/random.rb, line 280
280:     def rand_pair
281:       k = rand_key
282:       return k, fetch(k)
283:     end
rand_pair!() click to toggle source

Deletes a random key-value pair and returns that pair.

  a = {:one => 1, :two => 2, :three => 3}
  a.rand_pair!  #=> [:two, 2]
  a             #=> {:one => 1, :three => 3}
     # File lib/more/facets/random.rb, line 291
291:     def rand_pair!
292:       k,v = rand_pair
293:       delete( k )
294:       return k,v
295:     end
Also aliased as: pick_pair
rand_value() click to toggle source

Returns a random hash value.

  {:one => 1, :two => 2, :three => 3}.rand_value  #=> 2
  {:one => 1, :two => 2, :three => 3}.rand_value  #=> 1
     # File lib/more/facets/random.rb, line 304
304:     def rand_value
305:       fetch(rand_key)
306:     end
Also aliased as: at_rand
rand_value!() click to toggle source

Deletes a random key-value pair and returns the value.

  a = {:one => 1, :two => 2, :three => 3}
  a.at_rand!  #=> 2
  a           #=> {:one => 1, :three => 3}
     # File lib/more/facets/random.rb, line 314
314:     def rand_value!
315:       k,v = rand_pair
316:       delete( k )
317:       return v
318:     end
Also aliased as: pick, at_rand!
shuffle() click to toggle source

Returns a copy of the hash with values arranged in new random order.

  h = {:a=>1, :b=>2, :c=>3}
  h.shuffle_hash  #=> {:b=>2, :c=>1, :a>3}
     # File lib/more/facets/random.rb, line 331
331:     def shuffle
332:       ::Hash.zipnew( keys.sort_by{Random.number}, values.sort_by{Random.number} )
333:     end
shuffle!() click to toggle source

Destructive shuffle_hash. Arrange the values in a new random order.

  h = {:a => 1, :b => 2, :c => 3}
  h.shuffle_hash!
  h  #=> {:b=>2, :c=>1, :a=>3}
     # File lib/more/facets/random.rb, line 342
342:     def shuffle!
343:       self.replace(shuffle)
344:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.