On Jan 23, 2007, at 11:25 AM, Phrogz wrote:

> WKC CCC wrote:
>> Does anyone know of a function that multiplies the contents of an  
>> array.
>
> module Enumerable
>   def product
>     inject{ |piece, prod| prod*piece }
>   end
>   def sum
>     inject(0){ |piece, total| total+piece }
>   end
> end
>
> a = (1..10)
> p a.sum
> #=> 55
> p a.product
> #=> 3628800

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.

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.

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com