Sha256: 98b05135df77119df931bb6cf4fbb13f3cc0f9405a998ccfcb3a36655ff3a951
Contents?: true
Size: 932 Bytes
Versions: 26
Compression:
Stored size: 932 Bytes
Contents
class Array # Return a random element of the array. # # [1, 2, 3, 4].at_rand #=> 2 # [1, 2, 3, 4].at_rand #=> 4 # def at_rand self.at( rand( size ) ) end # Same as #at_rand, but acts in place removing a # random element from the array. # # a = [1,2,3,4] # a.at_rand! #=> 2 # a #=> [1,3,4] # def at_rand! return delete_at( Kernel.rand( size ) ) end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCArray < Test::Unit::TestCase def test_at_rand a = [1,2,3,4,5] 20.times{ assert_nothing_raised{ a.at_rand } } 20.times{ assert( a.include?( a.at_rand ) ) } end def test_at_rand! a = ['a','b','c'] assert_equal( 1, a.at_rand!.length ) assert_equal( 2, a.length ) end end =end
Version data entries
26 entries across 26 versions & 1 rubygems