@@ -505,43 +505,53 @@ 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
-
524
- # Do the sym version
523
+ # inject(initial_operand, symbol) { ... }
524
+ if block_given? && !Primitive . undefined? ( sym )
525
+ warn 'given block not used' , uplevel : 1
526
+ end
525
527
526
- sym = sym . to_sym
528
+ if Primitive . undefined? ( sym ) && block_given?
529
+ # Do the block version:
530
+ # inject {|memo, operand| ... } -> object
531
+ # inject(initial_operand) {|memo, operand| ... } -> object
527
532
528
533
each do
529
534
o = Primitive . single_block_arg
530
535
if Primitive . undefined? initial
531
536
initial = o
532
537
else
533
- initial = initial . __send__ ( sym , o )
538
+ initial = yield ( initial , o )
534
539
end
535
540
end
536
-
537
- # Block version
538
541
else
542
+ # Do the sym version:
543
+ # inject(symbol) -> object
544
+ # inject(initial_operand, symbol) -> object
545
+
546
+ sym , initial = initial , undefined if Primitive . undefined? ( sym )
547
+ sym = sym . to_sym
548
+
539
549
each do
540
550
o = Primitive . single_block_arg
541
551
if Primitive . undefined? initial
542
552
initial = o
543
553
else
544
- initial = yield ( initial , o )
554
+ initial = initial . __send__ ( sym , o )
545
555
end
546
556
end
547
557
end
0 commit comments