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

Commits on Oct 26, 2013

  1. Cleanup Hash#each

    meh committed Oct 26, 2013
    Copy the full SHA
    a6d458b View commit details
  2. Copy the full SHA
    5471590 View commit details
  3. Copy the full SHA
    0d7dc0f View commit details
  4. Copy the full SHA
    94373bd View commit details
  5. Copy the full SHA
    1bdb2b3 View commit details
Showing with 67 additions and 34 deletions.
  1. +43 −30 corelib/enumerable.rb
  2. +8 −4 corelib/hash.rb
  3. +5 −0 spec/opal/enumerable/detect_break_spec.rb
  4. +5 −0 spec/opal/enumerable/drop_while_break.rb
  5. +6 −0 spec/opal/enumerable/each_slice_break.rb
73 changes: 43 additions & 30 deletions corelib/enumerable.rb
Original file line number Diff line number Diff line change
@@ -185,23 +185,25 @@ def detect(ifnone = undefined, &block)
end

def drop(number)
number = Opal.coerce_to number, Integer, :to_int

if `number < 0`
raise ArgumentError, "attempt to drop negative size"
end

%x{
var result = [],
current = 0;
if (number < 0) {
#{raise ArgumentError};
}
#{self}.$each._p = function(e) {
self.$each._p = function() {
if (number < current) {
result.push(e);
result.push(#{Opal.destructure(`arguments`)});
}
current++;
};
#{self}.$each()
self.$each()
return result;
}
@@ -213,56 +215,67 @@ def drop_while(&block)
%x{
var result = [];
#{self}.$each._p = function() {
var value;
var param = arguments.length == 1 ?
arguments[0] : $slice.call(arguments);
self.$each._p = function() {
var param = #{Opal.destructure(`arguments`)},
value = $opal.$yield1(block, param);
if ((value = block(param)) === $breaker) {
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (value === false || value === nil) {
result.push(param);
return value;
if (#{Opal.truthy?(`value`)}) {
return;
}
return $breaker;
result.push(param);
};
#{self}.$each();
self.$each();
return result;
}
end

def each_slice(n, &block)
n = Opal.coerce_to n, Integer, :to_int

return enum_for :each_slice, n unless block_given?

%x{
var all = [];
var result,
slice = []
#{self}.$each._p = function() {
var param = arguments.length == 1 ?
arguments[0] : $slice.call(arguments);
self.$each._p = function() {
var param = #{Opal.destructure(`arguments`)};
all.push(param);
slice.push(param);
if (all.length == n) {
block(all.slice(0));
all = [];
if (slice.length === n) {
if (block(slice) === $breaker) {
result = $breaker.$v;
return $breaker;
}
slice = [];
}
};
#{self}.$each();
self.$each();
// our "last" group, if smaller than n then wont have been yielded
if (all.length > 0) {
block(all.slice(0));
if (result !== undefined) {
return result;
}
return nil;
// our "last" group, if smaller than n then won't have been yielded
if (slice.length > 0) {
if (block(slice) === $breaker) {
return $breaker.$v;
}
}
}

nil
end

def each_with_index(&block)
12 changes: 8 additions & 4 deletions corelib/hash.rb
Original file line number Diff line number Diff line change
@@ -261,15 +261,19 @@ def each(&block)
return enum_for :each unless block

%x{
var map = #{self}.map, keys = #{self}.keys;
var map = self.map,
keys = self.keys;
for (var i = 0, length = keys.length; i < length; i++) {
var key = keys[i];
var key = keys[i],
value = $opal.$yield1(block, [key, map[key]]);
#{yield [`key`, `map[key]`]};
if (value === $breaker) {
return $breaker.$v;
}
}
return #{self};
return self;
}
end

5 changes: 5 additions & 0 deletions spec/opal/enumerable/detect_break_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe "Enumerable#detect" do
it "breaks out with the proper value" do
[1, 2, 3].detect { break 42 }.should == 42
end
end
5 changes: 5 additions & 0 deletions spec/opal/enumerable/drop_while_break.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe "Enumerable#drop_while" do
it "breaks out with the proper value" do
[1, 2, 3].drop_while { break 42 }.should == 42
end
end
6 changes: 6 additions & 0 deletions spec/opal/enumerable/each_slice_break.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe "Enumerable#each_slice" do
it "breaks out with the proper value" do
[1, 2, 3].each_slice(1) { break 42 }.should == 42
[1, 2, 3].each_slice(2) { break 42 }.should == 42
end
end