Skip to content

Commit

Permalink
Add support for symbol arguments in Enumerable#inject
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Oct 27, 2013
1 parent 2b06542 commit e730c75
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions corelib/enumerable.rb
Expand Up @@ -496,27 +496,50 @@ def include?(obj)
any? { |v| v == obj }
end

def inject(object = undefined, &block)
def inject(object = undefined, sym = undefined, &block)
%x{
var result = object;
self.$each._p = function() {
var value = #{Opal.destructure(`arguments`)};
if (block !== nil) {
self.$each._p = function() {
var value = #{Opal.destructure(`arguments`)};
if (result === undefined) {
result = value;
return;
}
value = $opal.$yieldX(block, [result, value]);
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (result === undefined) {
result = value;
return;
};
}
else {
if (sym === undefined) {
if (!#{Symbol === object}) {
#{raise TypeError, "#{object.inspect} is not a Symbol"};
}
sym = object;
result = undefined;
}
value = $opal.$yieldX(block, [result, value]);
self.$each._p = function() {
var value = #{Opal.destructure(`arguments`)};
if (value === $breaker) {
result = $breaker.$v;
return $breaker;
}
if (result === undefined) {
result = value;
return;
}
result = value;
};
result = #{`result`.__send__ sym, `value`};
};
}
self.$each();
Expand Down

0 comments on commit e730c75

Please sign in to comment.