Home Segments Index Top Previous Next

387: Mainline

Yet another, much more elegant way to add up the numbers in an array is to use the powerful inject:into: method, inherited from the Collection class, with a two-argument block:

an Array instance  
  inject: initial value  
  into: [:previousResult :nextElement | statements]   

Here is what happens when you send a inject:into: method: