Sha256: 3dea4efe942c4e4148a82afb5a998bcd5de47065b07e306be4a7e7e74e55ef23

Contents?: true

Size: 1.25 KB

Versions: 269

Compression:

Stored size: 1.25 KB

Contents

class Array
  # Backport of Array#sample based on Marc-Andre Lafortune's https://github.com/marcandre/backports/
  # Returns a random element or +n+ random elements from the array.
  # If the array is empty and +n+ is nil, returns <tt>nil</tt>.
  # If +n+ is passed and its value is less than 0, it raises an +ArgumentError+ exception.
  # If the value of +n+ is equal or greater than 0 it returns <tt>[]</tt>.
  #
  #   [1,2,3,4,5,6].sample     # => 4
  #   [1,2,3,4,5,6].sample(3)  # => [2, 4, 5]
  #   [1,2,3,4,5,6].sample(-3) # => ArgumentError: negative array size
  #              [].sample     # => nil
  #              [].sample(3)  # => []
  def sample(n=nil)
    return self[Kernel.rand(size)] if n.nil?
    n = n.to_int
  rescue Exception => e
    raise TypeError, "Coercion error: #{n.inspect}.to_int => Integer failed:\n(#{e.message})"
  else
    raise TypeError, "Coercion error: obj.to_int did NOT return an Integer (was #{n.class})" unless n.kind_of? Integer
    raise ArgumentError, "negative array size" if n < 0
    n = size if n > size
    result = Array.new(self)
    n.times do |i|
      r = i + Kernel.rand(size - i)
      result[i], result[r] = result[r], result[i]
    end
    result[n..size] = []
    result
  end unless method_defined? :sample
end

Version data entries

269 entries across 225 versions & 25 rubygems

Version Path
activesupport-3.2.10 lib/active_support/core_ext/array/random_access.rb
activesupport-3.1.9 lib/active_support/core_ext/array/random_access.rb
classiccms-0.6.9 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
jquery_regex-0.0.1 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/core_ext/array/random_access.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/core_ext/array/random_access.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/core_ext/array/random_access.rb
challah-rolls-0.2.0 vendor/bundle/gems/activesupport-3.2.7/lib/active_support/core_ext/array/random_access.rb
challah-rolls-0.2.0 vendor/bundle/gems/activesupport-3.2.8/lib/active_support/core_ext/array/random_access.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/activesupport-3.2.8/lib/active_support/core_ext/array/random_access.rb
challah-rolls-0.2.0 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/core_ext/array/random_access.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/activesupport-3.2.7/lib/active_support/core_ext/array/random_access.rb