What is the ruby equivalent of the Linq Aggregate Method. It works something like this.
var factorial = new[] { 1, 2, 3, 4, 5 }.Aggregate((acc, i) => acc * i);
the acc variable accumulates every time a value from an array sequence is passed to lambda ..
, . . Ruby Smalltalk, inject:into: ( aCollection inject: aStartValue into: aBlock.). , Ruby inject. reduce, , - .
inject:into:
aCollection inject: aStartValue into: aBlock.
inject
reduce
# Ruby:
factorial = [1, 2, 3, 4, 5].reduce(:*)
, , :
factorial = (1..5).reduce(:*) factorial = 1.upto(5).reduce(:*)
. # .
:
a = [1,2,3,4,5] factorial = a.inject(1) do |product, i| product * i end