From: Phrogz Date: 2007-01-24T01:50:12+09:00 Subject: Re: Multiplying array Rob Biedenharn wrote: > module Enumerable > def product > inject(1) { |prod, piece| prod*piece } > end > def sum > inject(0) { |total, piece| total+piece } > end > end > > True, except the block parameters were reversed. In these cases, the > final result wasn't affected because the operations are commutative. Man, I swear that every time I use #inject I get it backwards, and think that I've fixed the answer in my mind for the next time. Thanks for the correction. (Anyone got a good _why-like mnemonic they use for remembering the order?) > I also added the argument to the inject within product for symmetry. > You could equally remove the 0 argument from the sum method's use of > inject. I thought about that, but in my mind, the sum of an empty array is zero, but the product of an empty array is nil (or 0?), not 1. Of course, totally up the the OP as to how s/he wants to handle this edge case.