In my last blog post I illustrated a very simple usage of Ruby’s inject method (more specifically, Enumerable#inject) – summing the values in array. In the comments Wayne Conrad pointed out that there is an even simpler way of doing this and it got me thinking – how many different ways can you sum the values in an array?
Let’s start with the simplest case – a plain loop.
The way I’ve illustrated already is with the inject method.
If you’re using Ruby 1.9 you also get access to Symbol#to_proc (which was previously a helper in ActiveSupport) and you can do the following (please note, I haven’t tested it – I’m still using 1.8).
We can achieve the same with a block and the collect method.
We can also try to use a lambda, but as far as I can tell we will first need to extend the Array class to write an iterator that takes a lambda as a parameter.
Using this same iterator we can also pass a Proc.
Pretty cool! Obviously most of these examples are really convoluted, but I’m simply trying to explore the different ways of doing things. If you can think of any other ways (convoluted or not) of doing this, leave a comment below. If I’m doing something really stupid in any of the examples, please point it out – I’m obviously very new to Ruby.