Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 34e81f6b8b88
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 07365f3440d2
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Mar 10, 2015

  1. Verified

    This commit was signed with the committer’s verified signature.
    headius Charles Oliver Nutter
    Copy the full SHA
    ec9ba5d View commit details
  2. Merge pull request #2677 from bjfish/truffle_array_sample

    [Truffle] Adding Array#sample to array.rb
    chrisseaton committed Mar 10, 2015
    Copy the full SHA
    07365f3 View commit details
Showing with 46 additions and 20 deletions.
  1. +0 −20 spec/truffle/tags/core/array/sample_tags.txt
  2. +46 −0 truffle/src/main/ruby/core/rubinius/common/array.rb
20 changes: 0 additions & 20 deletions spec/truffle/tags/core/array/sample_tags.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
fails:Array#sample returns nil for an empty Array
fails:Array#sample returns a single value when not passed a count
fails:Array#sample returns an empty Array when passed zero
fails:Array#sample returns an Array of elements when passed a count
fails:Array#sample returns elements from the Array
fails:Array#sample returns at most the number of elements in the Array
fails:Array#sample does not return the same value if the Array has unique values
fails:Array#sample may return the same value if the array is not unique
fails:Array#sample calls #to_int to convert the count when passed an Object
fails:Array#sample raises ArgumentError when passed a negative count
fails:Array#sample does not return subclass instances with Array subclass
fails:Array#sample with options calls #to_hash to convert the passed Object
fails:Array#sample with options calls #to_int on the first argument and #to_hash on the second when passed Objects
fails:Array#sample with options calls #rand on the Object passed by the :random key in the arguments Hash
fails:Array#sample with options ignores an Object passed for the RNG if it does not define #rand
fails:Array#sample with options when the object returned by #rand is a Fixnum uses the fixnum as index
fails:Array#sample with options when the object returned by #rand is a Fixnum raises a RangeError if the value is less than zero
fails:Array#sample with options when the object returned by #rand is a Fixnum raises a RangeError if the value is equal to the Array size
fails:Array#sample when the object returned by #rand is not a Fixnum but responds to #to_int calls #to_int on the Object
fails:Array#sample when the object returned by #rand is not a Fixnum but responds to #to_int raises a RangeError if the value is less than zero
fails:Array#sample when the object returned by #rand is not a Fixnum but responds to #to_int raises a RangeError if the value is equal to the Array size
46 changes: 46 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/array.rb
Original file line number Diff line number Diff line change
@@ -505,6 +505,52 @@ def rotate!(cnt=1)
replace ary
end

def sample(count=undefined, options=undefined)
return at Kernel.rand(size) if undefined.equal? count

if undefined.equal? options
if o = Rubinius::Type.check_convert_type(count, Hash, :to_hash)
options = o
count = nil
else
options = nil
count = Rubinius::Type.coerce_to_collection_index count
end
else
count = Rubinius::Type.coerce_to_collection_index count
options = Rubinius::Type.coerce_to options, Hash, :to_hash
end

if count and count < 0
raise ArgumentError, "count must be greater than 0"
end

rng = options[:random] if options
rng = Kernel unless rng and rng.respond_to? :rand

unless count
random = Rubinius::Type.coerce_to_collection_index rng.rand(size)
raise RangeError, "random value must be >= 0" if random < 0
raise RangeError, "random value must be less than Array size" unless random < size

return at random
end

count = size if count > size
result = Array.new self
tuple = Rubinius::Mirror::Array.reflect(result).tuple

count.times do |i|
random = Rubinius::Type.coerce_to_collection_index rng.rand(size)
raise RangeError, "random value must be >= 0" if random < 0
raise RangeError, "random value must be less than Array size" unless random < size

tuple.swap i, random
end

return count == size ? result : result[0, count]
end

def find_index(obj=undefined)
super
end