@@ -505,24 +505,32 @@ def sort_by
505
505
sort_values . map! { |ary | ary . value }
506
506
end
507
507
508
+ # Enumerable#inject
509
+ # inject(symbol) -> object
510
+ # inject(initial_operand, symbol) -> object
511
+ # inject {|memo, operand| ... } -> object
512
+ # inject(initial_operand) {|memo, operand| ... } -> object
508
513
def inject ( initial = undefined , sym = undefined , &block )
509
514
if Array === self
510
515
return Primitive . array_inject ( self , initial , sym , block )
511
516
end
512
517
513
- if !Primitive . undefined? ( sym ) && block_given?
514
- warn 'given block not used' , uplevel : 1
518
+ # inject()
519
+ if !block_given? && Primitive . undefined? ( initial )
520
+ raise ArgumentError , 'no block or symbol given'
515
521
end
516
522
517
- if !block_given? or !Primitive . undefined? ( sym )
518
- if Primitive . undefined? ( sym )
519
- raise ArgumentError , 'no block or symbol given' if Primitive . undefined? ( initial )
520
- sym = initial
521
- initial = undefined
522
- end
523
+ # inject(initial_operand, symbol) { ... }
524
+ if block_given? && !Primitive . undefined? ( sym )
525
+ warn 'given block not used' , uplevel : 1
526
+ end
523
527
524
- # Do the sym version
528
+ unless Primitive . undefined? ( sym ) && block_given?
529
+ # Do the sym version:
530
+ # inject(symbol) -> object
531
+ # inject(initial_operand, symbol) -> object
525
532
533
+ sym , initial = initial , undefined if Primitive . undefined? ( sym )
526
534
sym = sym . to_sym
527
535
528
536
each do
@@ -534,7 +542,9 @@ def inject(initial=undefined, sym=undefined, &block)
534
542
end
535
543
end
536
544
537
- # Block version
545
+ # Do the block version:
546
+ # inject {|memo, operand| ... } -> object
547
+ # inject(initial_operand) {|memo, operand| ... } -> object
538
548
else
539
549
each do
540
550
o = Primitive . single_block_arg
0 commit comments