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

Version Path
facets-1.0.3 packages/core/lib/facet/array/at_rand.rb
facets-0.9.0 lib/nano/array/at_rand.rb
facets-1.0.0 lib/facet/array/at_rand.rb
facets-1.3.0 lib/facets/core/array/at_rand.rb
facets-1.2.0 lib/facets/core/array/at_rand.rb
facets-1.2.1 lib/facets/core/array/at_rand.rb
facets-1.1.0 lib/facet/array/at_rand.rb
facets-1.3.1 lib/facets/core/array/at_rand.rb
facets-1.3.2 lib/facets/core/array/at_rand.rb
facets-1.3.3 lib/facets/core/array/at_rand.rb
facets-1.4.0 lib/facets/core/array/at_rand.rb
facets-1.4.1 lib/facets/core/array/at_rand.rb
facets-1.4.2 lib/facets/core/array/at_rand.rb
facets-1.4.3 lib/facets/core/array/at_rand.rb
facets-1.4.4 lib/facets/core/array/at_rand.rb
facets-1.4.5 lib/facets/core/array/at_rand.rb
facets-1.7.0 lib/facets/core/array/at_rand.rb
facets-1.7.30 lib/facets/core/array/at_rand.rb
facets-1.7.38 lib/facets/core/array/at_rand.rb
facets-1.7.46 lib/facets/core/array/at_rand.rb