From: Roelof Wobben Date: 2012-09-28T00:53:01+09:00 Subject: Re: subtract values of a array --_8a6cf81c-4189-4225-bf5f-ba88d3ff819f_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Thanks for all the help. And overthinking is a well known problem for me. Roelof > Date: Fri=2C 28 Sep 2012 00:44:26 +0900 > From: rubytalk2dave@davearonson.com > Subject: Re: subtract values of a array > To: ruby-talk@ruby-lang.org >=20 > On Thu=2C Sep 27=2C 2012 at 11:36 AM=2C Roelof Wobben wrote: >=20 > > def subtract(*numbers) > > numbers.inject(0) { |sum=2C number| sum - number } > > end > > > > but that won't work subtract ( 4=2C5) gives -9 when -1 is the right a= nswer. >=20 > You're overthinking it. Look at a simpler usage of inject. >=20 > I find it useful to think of inject as injecting the operation between > each element of the array it's given=2C and if it's given an argument as > well (not an operation=2C one to treat as a starting value)=2C then > between that and the start as well. So what you've done there is tell > it to do=2C essentially=2C 0 - 4 - 5. All you want is 4 - 5. How would > you eliminate the "0 -" part? Hint: the argument is optional. >=20 > -Dave >=20 > --=20 > Dave Aronson=2C T. Rex of Codosaurus=2C LLC... aka > Available Secret-Cleared Ruby/Rails Freelancer > (NoVa/DC/Remote)=3B see http://www.Codosaur.us/. >=20 = --_8a6cf81c-4189-4225-bf5f-ba88d3ff819f_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Thanks for all the help.
And overthinking is a well known problem for me= .

Roelof


>= =3B Date: Fri=2C 28 Sep 2012 00:44:26 +0900
>=3B From: rubytalk2dave@d= avearonson.com
>=3B Subject: Re: subtract values of a array
>=3B = To: ruby-talk@ruby-lang.org
>=3B
>=3B On Thu=2C Sep 27=2C 2012 a= t 11:36 AM=2C Roelof Wobben <=3Brwobben@hotmail.com>=3B wrote:
>= =3B
>=3B >=3B def subtract(*numbers)
>=3B >=3B numbers.inj= ect(0) { |sum=2C number| sum - number }
>=3B >=3B end
>=3B >= =3B
>=3B >=3B but that won't work subtract ( 4=2C5) gives -9 when = -1 is the right answer.
>=3B
>=3B You're overthinking it. Look = at a simpler usage of inject.
>=3B
>=3B I find it useful to thin= k of inject as injecting the operation between
>=3B each element of th= e array it's given=2C and if it's given an argument as
>=3B well (not = an operation=2C one to treat as a starting value)=2C then
>=3B between= that and the start as well. So what you've done there is tell
>=3B i= t to do=2C essentially=2C 0 - 4 - 5. All you want is 4 - 5. How would
= >=3B you eliminate the "0 -" part? Hint: the argument is optional.
&g= t=3B
>=3B -Dave
>=3B
>=3B --
>=3B Dave Aronson=2C T.= Rex of Codosaurus=2C LLC... aka
>=3B Available Secret-Cleared Ruby/Ra= ils Freelancer
>=3B (NoVa/DC/Remote)=3B see http://www.Codosaur.us/.>=3B
= --_8a6cf81c-4189-4225-bf5f-ba88d3ff819f_--