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: da897df810f7
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 63f0cf3cd81d
Choose a head ref
  • 7 commits
  • 7 files changed
  • 1 contributor

Commits on Nov 10, 2013

  1. Make String#end_with? compliant

    meh committed Nov 10, 2013

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    77e8cce View commit details
  2. Make String#start_with? compliant

    meh committed Nov 10, 2013
    Copy the full SHA
    03272d5 View commit details
  3. Copy the full SHA
    c9526f3 View commit details
  4. Copy the full SHA
    67dffa4 View commit details
  5. Implement Enumerable#cycle

    meh committed Nov 10, 2013
    Copy the full SHA
    a573ac0 View commit details
  6. Fix off by one in Enumerable#drop

    meh committed Nov 10, 2013
    Copy the full SHA
    f8b910d View commit details
  7. Copy the full SHA
    63f0cf3 View commit details
Showing with 109 additions and 33 deletions.
  1. +83 −12 opal/core/enumerable.rb
  2. +4 −2 opal/core/string.rb
  3. +15 −10 opal/opal.rb
  4. +4 −0 spec/filters/20.rb
  5. +1 −3 spec/filters/bugs/enumerable.rb
  6. +0 −6 spec/filters/bugs/enumerator.rb
  7. +2 −0 spec/filters/unsupported/enumerator.rb
95 changes: 83 additions & 12 deletions opal/core/enumerable.rb
Original file line number Diff line number Diff line change
@@ -122,6 +122,71 @@ def count(object = undefined, &block)
}
end

def cycle(n = nil, &block)
return enum_for :cycle, n unless block

unless n.nil?
n = Opal.coerce_to! n, Integer, :to_int

return if `n <= 0`
end

%x{
var result,
all = [];
self.$each._p = function() {
var param = #{Opal.destructure(`arguments`)},
value = $opal.$yield1(block, param);
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
all.push(param);
}
self.$each();
if (result !== undefined) {
return result;
}
if (all.length === 0) {
return nil;
}
}

if n.nil?
%x{
while (true) {
for (var i = 0, length = all.length; i < length; i++) {
var value = $opal.$yield1(block, all[i]);
if (value === $breaker) {
return $breaker.$v;
}
}
}
}
else
%x{
while (n > 1) {
for (var i = 0, length = all.length; i < length; i++) {
var value = $opal.$yield1(block, all[i]);
if (value === $breaker) {
return $breaker.$v;
}
}
n--;
}
}
end
end

def detect(ifnone = undefined, &block)
return enum_for :detect, ifnone unless block_given?

@@ -170,7 +235,7 @@ def drop(number)
current = 0;
self.$each._p = function() {
if (number < current) {
if (number <= current) {
result.push(#{Opal.destructure(`arguments`)});
}
@@ -187,22 +252,28 @@ def drop_while(&block)
return enum_for :drop_while unless block_given?

%x{
var result = [];
var result = [],
dropping = true;
self.$each._p = function() {
var param = #{Opal.destructure(`arguments`)},
value = $opal.$yield1(block, param);
var param = #{Opal.destructure(`arguments`)};
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (dropping) {
var value = $opal.$yield1(block, param);
if (#{Opal.truthy?(`value`)}) {
return;
}
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
result.push(param);
if (#{Opal.falsy?(`value`)}) {
dropping = false;
result.push(param);
}
}
else {
result.push(param);
}
};
self.$each();
6 changes: 4 additions & 2 deletions opal/core/string.rb
Original file line number Diff line number Diff line change
@@ -267,7 +267,7 @@ def empty?
def end_with?(*suffixes)
%x{
for (var i = 0, length = suffixes.length; i < length; i++) {
var suffix = suffixes[i];
var suffix = #{Opal.coerce_to `suffixes[i]`, String, :to_str};
if (self.length >= suffix.length && self.substr(0 - suffix.length) === suffix) {
return true;
@@ -563,7 +563,9 @@ def split(pattern = $; || ' ', limit = undefined)
def start_with?(*prefixes)
%x{
for (var i = 0, length = prefixes.length; i < length; i++) {
if (#{self}.indexOf(prefixes[i]) === 0) {
var prefix = #{Opal.coerce_to `prefixes[i]`, String, :to_str};
if (self.indexOf(prefix) === 0) {
return true;
}
}
25 changes: 15 additions & 10 deletions opal/opal.rb
Original file line number Diff line number Diff line change
@@ -28,21 +28,26 @@
$& = $~ = $` = $' = nil

# stub library path
$: = []
$: = []
$" = []

# split lines
$/ = "\n"
$, = " "
$/ = "\n"
$, = " "

ARGV = []
ARGF = Object.new
ENV = {}
ARGV = []
ARGF = Object.new
ENV = {}

RUBY_PLATFORM = 'opal'
RUBY_ENGINE = 'opal'
RUBY_VERSION = '1.9.3'
$VERBOSE = false
$DEBUG = false
$SAFE = 0

RUBY_PLATFORM = 'opal'
RUBY_ENGINE = 'opal'
RUBY_VERSION = '1.9.3'
RUBY_ENGINE_VERSION = '0.5.1'
RUBY_RELEASE_DATE = '2013-11-10'
RUBY_RELEASE_DATE = '2013-08-13'

module Opal
def self.coerce_to(object, type, method)
4 changes: 4 additions & 0 deletions spec/filters/20.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
opal_filter "2.0 behaviour" do
fails "Enumerator.new ignores block if arg given"
fails "String#end_with? ignores arguments not convertible to string"
end
4 changes: 1 addition & 3 deletions spec/filters/bugs/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
opal_filter "Enumerable" do
fails "Enumerable#drop passed a number n as an argument tries to convert n to an Integer using #to_int"

fails "Enumerable#drop_while passes elements to the block until the first false"
fails "Enumerable#cycle passed a number n as an argument raises an ArgumentError if more arguments are passed"

fails "Enumerable#each_slice raises an Argument Error if there is not a single parameter > 0"

6 changes: 0 additions & 6 deletions spec/filters/bugs/enumerator.rb

This file was deleted.

2 changes: 2 additions & 0 deletions spec/filters/unsupported/enumerator.rb
Original file line number Diff line number Diff line change
@@ -8,4 +8,6 @@
fails "Enumerator#rewind has no effect on a new enumerator"
fails "Enumerator#rewind has no effect if called multiple, consecutive times"
fails "Enumerator#rewind does nothing if the object doesn't have a #rewind method"
fails "Enumerator#rewind works with peek to reset the position"
fails "Enumerator#rewind calls the enclosed object's rewind method if one exists"
end