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: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ea0e2c5fd4c3
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7ed0538191d4
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on May 15, 2015

  1. RubySpec: Array#rotate

    kaiwren committed May 15, 2015
    Copy the full SHA
    901e367 View commit details
  2. RubySpec: Array#rotate!

    kaiwren committed May 15, 2015
    Copy the full SHA
    cb5e235 View commit details
  3. Merge pull request #861 from c42engineering/rubyspec_array_rotate

    RubySpec: Array#rotate and Array#rotate!
    vais committed May 15, 2015
    Copy the full SHA
    7ed0538 View commit details
Showing with 34 additions and 15 deletions.
  1. +34 −0 opal/corelib/array.rb
  2. +0 −15 spec/filters/bugs/array.rb
34 changes: 34 additions & 0 deletions opal/corelib/array.rb
Original file line number Diff line number Diff line change
@@ -1427,7 +1427,41 @@ def rindex(object = undefined, &block)
return nil;
}
end

def rotate(n=1)
n = Opal.coerce_to n, Integer, :to_int
%x{
var ary, idx, firstPart, lastPart;
if (self.length === 1) {
return self.slice();
}
if (self.length === 0) {
return [];
}
ary = self.slice();
idx = n % ary.length;
firstPart = ary.slice(idx);
lastPart = ary.slice(0, idx);
return firstPart.concat(lastPart);
}
end

def rotate!(cnt=1)
raise RuntimeError, "can't modify frozen Array" if frozen?

%x{
if (self.length === 0 || self.length === 1) {
return self;
}
}
cnt = Opal.coerce_to cnt, Integer, :to_int
ary = rotate(cnt)
replace ary
end

def sample(n = nil)
return nil if !n && empty?
return [] if n && empty?
15 changes: 0 additions & 15 deletions spec/filters/bugs/array.rb
Original file line number Diff line number Diff line change
@@ -81,21 +81,6 @@

fails "Array#rindex rechecks the array size during iteration"

fails "Array#rotate! with an argument n raises a TypeError if not passed an integer-like argument"
fails "Array#rotate! with an argument n coerces the argument using to_int"
fails "Array#rotate! with an argument n moves the first (n % size) elements at the end and returns self"
fails "Array#rotate! when passed no argument moves the first element to the end and returns self"
fails "Array#rotate! raises a RuntimeError on a frozen array"
fails "Array#rotate! does nothing and returns self when the length is zero or one"
fails "Array#rotate with an argument n raises a TypeError if not passed an integer-like argument"
fails "Array#rotate with an argument n coerces the argument using to_int"
fails "Array#rotate with an argument n returns a copy of the array with the first (n % size) elements moved at the end"
fails "Array#rotate when passed no argument returns a copy of the array with the first element moved at the end"
fails "Array#rotate does not return subclass instance for Array subclasses"
fails "Array#rotate does not return self"
fails "Array#rotate does not mutate the receiver"
fails "Array#rotate returns a copy of the array when its length is one or zero"

fails "Array#sample calls #rand on the Object passed by the :random key in the arguments Hash"
fails "Array#sample calls #to_hash to convert the passed Object"
fails "Array#sample calls #to_int on the Object returned by #rand"