Sha256: 87d59ef055e032118de3b11572c9f6a0ce1c997b5488539a401f53df148cfd1f
Contents?: true
Size: 855 Bytes
Versions: 1
Compression:
Stored size: 855 Bytes
Contents
require 'nano/hash/rand_key' class Hash # Returns a random key-value pair. # # {: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. # # 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! ) end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCHash < Test::Unit::TestCase def test_rand_pair h = { :a=>1, :b=>2, :c=>3 } 10.times { k,v = *h.rand_pair; assert_equal( v, h[k] ) } end end =end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
facets-0.9.0 | lib/nano/hash/rand_pair.rb |