There already is a standard method for this:

irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> a.reduce(0) { |a,b| a+b }
=> 10
irb(main):003:0> a = []
=> []
irb(main):004:0> a.reduce(0) { |a,b| a+b }
=> 0


(Btw, "reduce" is the same as "inject", but I prefer the former as this
describes the behaviour, and doesn't allude to an infix syntax.)

-- 
Posted via http://www.ruby-forum.com/.