Jan 01 2011

Learn a bit of Ruby every day: Enumeration.reduce

Category: me,rubygiordano scalzo @ 11:30 pm

Ilya Grigorik is an awesome programmer who recently launched the geek equivalent of Farmville: VimGolf, the idea that every geek had, but nobody created.

From today he starts to tweet a piece of Ruby every day, mainly 1.9.2.

The first tweet is about Enumeration.reduce, an alternative, and more speaking, name for Enumeration.inject:
ruby-1.9.2-p136 > (1..3).reduce(:+)
=> 6

The strange argument is the symbol for plus, the operation to do to each element.
A more verbose, and less magic, version is:
ruby-1.9.2-p136 > (1..3).reduce {|sum, e| sum+e}
=> 6