Skip to content

Commit

Permalink
Cleanup and compliancy fixes for Enumerable#first
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Oct 27, 2013
1 parent a3f011d commit 4994bc4
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions corelib/enumerable.rb
Expand Up @@ -422,36 +422,31 @@ def find_index(object = undefined, &block)

def first(number = undefined)
%x{
var result = [],
current = 0,
proc;
if (number == null) {
result = nil;
proc = function() {
result = arguments.length == 1 ?
arguments[0] : $slice.call(arguments);
var result = nil;
self.$each._p = function() {
result = #{Opal.destructure(`arguments`)};
return $breaker;
};
}
else {
proc = function() {
var current = 0,
result = [],
number = #{Opal.coerce_to number, Integer, :to_int};
self.$each._p = function() {
if (number <= current) {
return $breaker;
}
var param = arguments.length == 1 ?
arguments[0] : $slice.call(arguments);
result.push(param);
result.push(#{Opal.destructure(`arguments`)});
current++;
};
}
#{self}.$each._p = proc;
#{self}.$each();
self.$each();
return result;
}
Expand Down

0 comments on commit 4994bc4

Please sign in to comment.