Ruby equivalent of C # Linq Aggregate Method

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 ..

+5
source share
2 answers

, . . Ruby Smalltalk, inject:into: ( aCollection inject: aStartValue into: aBlock.). , Ruby inject. reduce, , - .

# Ruby:

factorial = [1, 2, 3, 4, 5].reduce(:*)

, , :

factorial = (1..5).reduce(:*)
factorial = 1.upto(5).reduce(:*)
+14

. # .

:

a = [1,2,3,4,5]
factorial = a.inject(1) do |product, i|
  product * i
end
+2

All Articles