From: Brandon Weaver Date: 2012-09-18T14:23:18+09:00 Subject: Re: how to sum element of array --047d7b10cb1575475204c9f311e4 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Sep 17, 2012 at 11:28 PM, Josh Cheek wrote: > 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, :+ > Granted and noted, nice catch --047d7b10cb1575475204c9f311e4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Mon, Sep 17, 2012 at 11:28 PM, Josh C= heek <josh.cheek@gmail.com> wrote:
On Mon, Sep 17, 2012 at 11:03 PM, Brandon Weaver <= keystonelemur@gmail.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 will be wrong when there are no elements.

array.inject 0, :+


Granted and noted, nice catch --047d7b10cb1575475204c9f311e4--