From: Roelof Wobben Date: 2012-09-28T18:39:22+09:00 Subject: Re: calcaulation with unknown numbers of numbers and options fail --_64738c54-e2fb-4ef9-be9f-390ece2b0d1e_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > Date: Fri=2C 28 Sep 2012 18:25:22 +0900 > From: lists@ruby-forum.com > Subject: Re: calcaulation with unknown numbers of numbers and options fai= l > To: ruby-talk@ruby-lang.org >=20 > Roelof Wobben wrote in post #1077868: > > Can you explain the how the operator and the reduce work. >=20 > First of all=2C I most correct the code: It should be >=20 > operator =3D options.empty? || options[:add] ? :+ : :- >=20 > to make addition the default. >=20 > What I've written down is simply the short form of "inject": >=20 > http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-inject >=20 > Instead of passing a block=2C you can also pass a method name or operator > (as a symbol). The elements will then be aggregated using this > method/operator. >=20 > So >=20 > [1=2C 2=2C 3].inject :+ >=20 > is the same as >=20 > [1=2C 2=2C 3].inject {|sum=2C element| sum + element} >=20 > But it's obviously shorter. When you use the short form=2C it's common to > use the method name "reduce" instead of "inject" (but both are the same > method). >=20 > And the "operator" variable is used to specify the operator for "reduce" > according to the option hash. >=20 > --=20 > Posted via http://www.ruby-forum.com/. Thanks=20 So if I understand this well then you are saying when options is empty or = options =3D add then add the numbers otherwise subtract the numbers ? Roelof = --_64738c54-e2fb-4ef9-be9f-390ece2b0d1e_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable


>=3B Date: Fri=2C 28 S= ep 2012 18:25:22 +0900
>=3B From: lists@ruby-forum.com
>=3B Subje= ct: Re: calcaulation with unknown numbers of numbers and options fail
&g= t=3B To: ruby-talk@ruby-lang.org
>=3B
>=3B Roelof Wobben wrote i= n post #1077868:
>=3B >=3B Can you explain the how the operator and = the reduce work.
>=3B
>=3B First of all=2C I most correct the co= de: It should be
>=3B
>=3B operator =3D options.empty? || option= s[:add] ? :+ : :-
>=3B
>=3B to make addition the default.
>= =3B
>=3B What I've written down is simply the short form of "inject":=
>=3B
>=3B http://ruby-doc.org/core-1.9.3/Enumerable.html#method= -i-inject
>=3B
>=3B Instead of passing a block=2C you can also p= ass a method name or operator
>=3B (as a symbol). The elements will th= en be aggregated using this
>=3B method/operator.
>=3B
>=3B= So
>=3B
>=3B [1=2C 2=2C 3].inject :+
>=3B
>=3B is th= e same as
>=3B
>=3B [1=2C 2=2C 3].inject {|sum=2C element| sum += element}
>=3B
>=3B But it's obviously shorter. When you use the= short form=2C it's common to
>=3B use the method name "reduce" instea= d of "inject" (but both are the same
>=3B method).
>=3B
>= =3B And the "operator" variable is used to specify the operator for "reduce= "
>=3B according to the option hash.
>=3B
>=3B --
>= =3B Posted via http://www.ruby-forum.com/.


Thanks
So if I un= derstand this well then you are saying when =3B options is empty or opt= ions =3D add then =3B add the numbers otherwise subtract the numbers ?<= br>
Roelof


= --_64738c54-e2fb-4ef9-be9f-390ece2b0d1e_--