From: Per-erik Martin Date: 2013-06-25T15:49:27+09:00 Subject: Re: Sum method through while loops or iteration 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/.