Skip to content

Commit

Permalink
Make Enumerable#first compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Nov 11, 2013
1 parent c2e533b commit 337e9bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
40 changes: 26 additions & 14 deletions opal/core/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,35 +468,47 @@ def find_index(object = undefined, &block)
end

def first(number = undefined)
%x{
if (number == null) {
var result = nil;
if `number === undefined`
result = nil

%x{
self.$each._p = function() {
result = #{Opal.destructure(`arguments`)};
return $breaker;
};
self.$each();
}
else {
else
result = []
number = Opal.coerce_to number, Integer, :to_int

if `number < 0`
raise ArgumentError, 'attempt to take negative size'
end

if `number == 0`
return []
end

%x{
var current = 0,
result = [],
number = #{Opal.coerce_to number, Integer, :to_int};
self.$each._p = function() {
if (number <= current) {
return $breaker;
}
result.push(#{Opal.destructure(`arguments`)});
current++;
if (number <= ++current) {
return $breaker;
}
};
}
self.$each();
self.$each();
}
end

return result;
}
result
end

def grep(pattern, &block)
Expand Down
5 changes: 0 additions & 5 deletions spec/filters/bugs/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
opal_filter "Enumerable" do
fails "Enumerable#cycle passed a number n as an argument raises an ArgumentError if more arguments are passed"

fails "Enumerable#first when passed an argument consumes only what is needed"
fails "Enumerable#first when passed an argument raises a TypeError if the passed argument is not numeric"
fails "Enumerable#first when passed an argument tries to convert the passed argument to an Integer using #to_int"
fails "Enumerable#first when passed an argument raises an ArgumentError when count is negative"

fails "Enumerable#grep can use $~ in the block when used with a Regexp"

fails "Enumerable#group_by returns a hash without default_proc"
Expand Down

0 comments on commit 337e9bc

Please sign in to comment.