Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #359 from MagLev/johnnyt/sample
Browse files Browse the repository at this point in the history
Adds Array#sample for the basic use case (single random element)
  • Loading branch information
jc00ke committed Nov 12, 2014
2 parents 0276351 + a351466 commit 1ab6db3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 5 additions & 8 deletions spec/tags/core/array/sample_tags.txt
@@ -1,22 +1,19 @@
fails:Array#sample selects a random value from the array
fails:Array#sample returns nil for empty arrays
fails:Array#sample passed a number n as an argument raises ArgumentError for a negative n
fails:Array#sample passed a number n as an argument returns different random values from the array
fails:Array#sample passed a number n as an argument tries to convert n to an Integer using #to_int
fails:Array#sample passed a number n as an argument returns all values with n big enough
fails:Array#sample passed a number n as an argument returns [] for empty arrays or if n <= 0
fails:Array#sample passed a number n as an argument does not return subclass instances with Array subclass
fails:Array#sample selects a value from the array
fails:Array#sample returns a distribution of results
fails:Array#sample returns nil for empty arrays
fails:Array#sample returns only one element
fails:Array#sample returns proper array size
fails:Array#sample attempts coercion via #to_hash
fails:Array#sample uses default random generator
fails:Array#sample uses given random generator
fails:Array#sample uses default random generator and return proper array size
fails:Array#sample uses given random generator and return proper array size
fails:Array#sample passed a number n as an argument raises ArgumentError for a negative n
fails:Array#sample passed a number n as an argument selects values from the array
fails:Array#sample passed a number n as an argument does not return the same value if the array is unique
fails:Array#sample passed a number n as an argument may return the same value if the array is not unique
fails:Array#sample passed a number n as an argument returns a distribution of results
fails:Array#sample passed a number n as an argument tries to convert n to an Integer using #to_int
fails:Array#sample passed a number n as an argument returns all values when n >= array size
fails:Array#sample passed a number n as an argument returns [] for empty arrays or if n <= 0
fails:Array#sample passed a number n as an argument does not return subclass instances with Array subclass
7 changes: 7 additions & 0 deletions src/kernel/bootstrap/Array.rb
Expand Up @@ -1513,6 +1513,13 @@ def rindex(element)

primitive_nobridge '__remove_from_to_', 'removeFrom:to:'

# Let's get the basic use case working - just pulling a single random element
# Check out https://github.com/rubinius/rubinius/blob/master/kernel/common/array.rb#L1286 for
# the Rubinius implementation
def sample(count=MaglevUndefined, options=MaglevUndefined)
return at Kernel.rand(size)
end

def shift(count=MaglevUndefined) # added in 1.8.7
if count._equal?(MaglevUndefined)
return self.__shift
Expand Down

0 comments on commit 1ab6db3

Please sign in to comment.