Methods
Public Instance methods
sample(n=nil)

Backport of Array#sample based on Marc-Andre Lafortune‘s github.com/marcandre/backports/

    # File activesupport/lib/active_support/core_ext/array/random_access.rb, line 22
22:         def sample(n=nil)
23:           return self[Kernel.rand(size)] if n.nil?
24:           n = n.to_int
25:         rescue Exception => e
26:           raise TypeError, "Coercion error: #{n.inspect}.to_int => Integer failed:\n(#{e.message})"
27:         else
28:           raise TypeError, "Coercion error: #{n}.to_int did NOT return an Integer (was #{n.class})" unless n.kind_of? ::Integer
29:           raise ArgumentError, "negative array size" if n < 0
30:           n = size if n > size
31:           result = ::Array.new(self)
32:           n.times do |i|
33:             r = i + Kernel.rand(size - i)
34:             result[i], result[r] = result[r], result[i]
35:           end
36:           result[n..size] = []
37:           result
38:         end