Skip to content

Commit e730c75

Browse files
committedOct 27, 2013
Add support for symbol arguments in Enumerable#inject
1 parent 2b06542 commit e730c75

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed
 

‎corelib/enumerable.rb

+35-12
Original file line numberDiff line numberDiff line change
@@ -496,27 +496,50 @@ def include?(obj)
496496
any? { |v| v == obj }
497497
end
498498

499-
def inject(object = undefined, &block)
499+
def inject(object = undefined, sym = undefined, &block)
500500
%x{
501501
var result = object;
502502
503-
self.$each._p = function() {
504-
var value = #{Opal.destructure(`arguments`)};
503+
if (block !== nil) {
504+
self.$each._p = function() {
505+
var value = #{Opal.destructure(`arguments`)};
506+
507+
if (result === undefined) {
508+
result = value;
509+
return;
510+
}
511+
512+
value = $opal.$yieldX(block, [result, value]);
513+
514+
if (value === $breaker) {
515+
result = $breaker.$v;
516+
return $breaker;
517+
}
505518
506-
if (result === undefined) {
507519
result = value;
508-
return;
520+
};
521+
}
522+
else {
523+
if (sym === undefined) {
524+
if (!#{Symbol === object}) {
525+
#{raise TypeError, "#{object.inspect} is not a Symbol"};
526+
}
527+
528+
sym = object;
529+
result = undefined;
509530
}
510531
511-
value = $opal.$yieldX(block, [result, value]);
532+
self.$each._p = function() {
533+
var value = #{Opal.destructure(`arguments`)};
512534
513-
if (value === $breaker) {
514-
result = $breaker.$v;
515-
return $breaker;
516-
}
535+
if (result === undefined) {
536+
result = value;
537+
return;
538+
}
517539
518-
result = value;
519-
};
540+
result = #{`result`.__send__ sym, `value`};
541+
};
542+
}
520543
521544
self.$each();
522545

0 commit comments

Comments
 (0)
Please sign in to comment.