How does it work?

You'll notice that I passed two arguments to the block: sum and i. The
sum initializes to the argument passed to the inject() method call, in this
case 0. As the array is iterated across the return value of the block is
returned as the next sum when the next element in the array is yielded as i.

In several Reddit comments as well as a note in the comments from Victor Kmita
I was reminded of the simplest format for this to work. When all that is being
done in the block is a single method call this can be simplified by passing the
method call in as a symbol and dropping the block entirely.

def sum(arr)
arr.inject(:+)
end
sum([1,2,3])
#=> 6

Complex Counting

Lets try a slightly more complex example. Lets take a sentence and return a
hash, indexed by word with a value of the count of occurences of that word in
the sentence.

You'll notice that in this case I have to explicitly call the count_hash at the
end of the block. This is because the count_hash[word] += 1 line will return
the value of word and that would (incorrectly) get passed as the next count_hash

An obvious improvement on this would be a more robust mechanism used to convert
the input str into an array. Splitting on whitespace is fine for this example.

Inverted Index

Another similar example to the word counting trick would be to do a reverse
index. An inverted index is a simple data structure which allows you to do a
lookup for a keyword and provides a list of locations where that word occurred.

This has many improvements and could stand to be refactored out into some other
methods as well as deal with case sensitivity ('round' != 'Round') and
special characters but for the purposes of demonstrating inject() it is good
enough!