Sum & Product of an Array


Category:
Scripts
Ruby
Requirements:
Seller:
Price:
-
System:
Rating:
4.2
License:

Description - Sum & Product of an Array



How to sum all the numbers in an array, or get the multiplication product of the same numbers, without resorting to an ugly for loop? Ruby`s inject to the rescue; this Snippet extends the array class with the methods sum and product, which can hereafter be used on any array in Ruby. Note that the parameter to inject is the initial value of s, the result, and thus obviously different depending on whether we`re doing addition or multiplication.



More in Ruby-Sum & Product of an Array

Inject Numbers Thus Obviously Different