sample

ary.sample â obj
ary.sample(random: rng) â obj
ary.sample(n) â new_ary
ary.sample(n, random: rng) â new_ary
Instance Public methods

Choose a random element or n random elements from the array.

The elements are chosen by using random and unique indices into the array in order to ensure that an element doesn't repeat itself unless the array already contained duplicate elements.

If the array is empty the first form returns nil and the second form returns an empty array.

The optional rng argument will be used as the random number generator.

a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
a.sample         #=> 7
a.sample(4)      #=> [6, 4, 2, 5]
doc_ruby_on_rails
2015-03-30 19:44:24
Comments
Leave a Comment

Please login to continue.