On Thu, Nov 20, 2014 at 8:41 AM, Augusts Bautra <augusts / creo.mobi> wrote:

> As I started.. use Inject
>
> def sum (*numbers)
>    numbers.inject {|sum, n| sum + n }
> end
>
> Inject initialises an empty (zero) receptacle for aggregated data for you.

No. Your approach is broken because

irb(main):001:0> [].inject {|sum, n| sum + n }
=> nil

> More at http://blog.jayfields.com/2008/03/ruby-inject.html

That has it correct in the first example.  You need to pass in the
neutral element of the operation:

irb(main):002:0> [].inject(0) {|sum, n| sum + n }
=> 0

Cheers

robert

-- 
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/