Quickly Sum the Elements in an Array

I'm really loving Ruby these days. A lot. Today's tip: quickly get a sum of all the elements inside an array, using our buddy from Enumerable: inject.

Take a look at the following code:

x = [1, 5, 3, 0, 10]
x.inject :+
=> 19 

So clean! Check out the full documentation for the inject/reduce methods on Enumerable. Learn them! Use them! Profit! $$$