From: Josh Cheek Date: 2012-09-18T13:28:29+09:00 Subject: Re: how to sum element of array --047d7b33d6d65c1d3704c9f2524a Content-Type: text/plain; charset=ISO-8859-1 On Mon, Sep 17, 2012 at 11:03 PM, Brandon Weaver wrote: > > array.inject(0) { |sum, i| sum+i } >> >> Cheers. >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > You could also use a shorter version of inject: > > array.inject(:+) > You still need to give it an initial value or it will be wrong when there are no elements. array.inject 0, :+ --047d7b33d6d65c1d3704c9f2524a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, Sep 17, 2012 at 11:03 PM, Brandon Weaver <keystonelemur@gmai= l.com> wrote:

=A0 array.inject(0) { |sum, i| sum+i }

Cheers.

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


You could also use a = shorter version of inject:

array.inject(:+)

You still need to give it an initial value or it wil= l be wrong when there are no elements.

array.inject 0, :+
--047d7b33d6d65c1d3704c9f2524a--