Sha256: 31ad0963649c6b0dd41e9f24cdf73aa61c2d2d07d8533cb1ec7dccba6d92b2fd

Contents?: true

Size: 520 Bytes

Versions: 3

Compression:

Stored size: 520 Bytes

Contents

#--
# Adapted from rand.rb by:
# * Ilmari Heikkinen <mailto:kig@misfiring.net>
# * Christian Neukirchen <mailto:chneukirchen@gmail.com>
#++
require 'facet/hash/rand_pair'
class Hash
  # 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( :pick, :at_rand! )
end

 

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.1 lib/facet/hash/rand_value%21.rb
facets-0.7.0 lib/facet/hash/rand_value%21.rb
facets-0.7.2 lib/facet/hash/rand_value%21.rb