lib/facet/hash/at_rand.rb in facets-0.6.3 vs lib/facet/hash/at_rand.rb in facets-0.7.0

- old
+ new

@@ -1,88 +1,10 @@ #-- # Adapted from rand.rb by: # * Ilmari Heikkinen <mailto:kig@misfiring.net> # * Christian Neukirchen <mailto:chneukirchen@gmail.com> #++ +require 'facet/hash/rand_value' class Hash - - # Returns a random key-value pair. - # - # require 'facet/hash/at_rand' - # - # {:one => 1, :two => 2, :three => 3}.at_rand #=> [:one, 1] - # - def rand_value - fetch( rand_key ) - end - alias( :at_rand, :rand_value ) - - # Deletes a random key-value pair and returns the value. - # - # require 'facet/hash/at_rand' - # - # a = {:one => 1, :two => 2, :three => 3} - # a.at_rand! #=> 2 - # a #=> {:one => 1, :three => 3} - # - def rand_value! - k,v = rand_pair - delete( k ) - return v - end - alias_method( :at_rand!, :rand_value! ) - alias_method( :pick, :at_rand! ) - - # Returns a random key. - # - # require 'facet/hash/at_rand' - # - # {:one => 1, :two => 2, :three => 3}.pick_key #=> :three - # - def rand_key - keys.at( rand(keys.size) ) - end - - # Delete a random key-value pair, returning the key. - # - # require 'facet/hash/at_rand' - # - # a = {:one => 1, :two => 2, :three => 3} - # a.pick_key! #=> :two - # a #=> {:one => 1, :three => 3} - # - def rand_key! - k,v = rand_pair - delete( k ) - return k - end - alias( :pick_key, :rand_key! ) - - # Returns a random key-value pair. - # - # require 'facet/hash/at_rand' - # - # {:one => 1, :two => 2, :three => 3}.pick #=> [:one, 1] - # - def rand_pair - k = rand_key - return k, fetch(k) - end - - # Deletes a random key-value pair and returns that pair. - # - # require 'facet/hash/at_rand' - # - # a = {:one => 1, :two => 2, :three => 3} - # a.rand_pair! #=> [:two, 2] - # a #=> {:one => 1, :three => 3} - # - def rand_pair! - k,v = rand_pair - delete( k ) - return k,v - end - alias_method( :pick_pair, :rand_pair! ) - + alias_method( :at_rand, :rand_value ) end -