From: Roelof Wobben Date: 2012-09-28T18:15:59+09:00 Subject: Re: calcaulation with unknown numbers of numbers and options fail --_baf90789-a6bb-49ab-b546-970a22e829a8_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > Date: Fri=2C 28 Sep 2012 18:08:52 +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 > Hi=2C >=20 > Roelof Wobben wrote in post #1077862: > > I can declare it as this def calculate (*arguments) > > So i will be one hash but then I have to figure out how to seperate the > > options part and the numbers parts. >=20 > Then why do you do it? Just set the parameter list according to the=20 > method call: >=20 > def calculate *numbers=2C options > ... > end >=20 > I'm also quite sure that you're not supposed to subtract the numbers=20 > from 0 but rather from each other. So calculate(1=2C 2=2C {}) should yiel= d 1=20 > - 2 =3D -1 and not 0 - 1 - 2 =3D -3. >=20 > In this case you must omit the "0" parameter for "inject". And you=20 > should use the short form: >=20 >=20 > def calculate *numbers=2C options > operator =3D options[:add] ? :+ : :- > numbers.reduce operator > end >=20 > puts calculate 3=2C 5=2C add: true > puts calculate 3=2C 5=2C add: false > puts calculate(3=2C 5=2C {}) >=20 > --=20 > Posted via http://www.ruby-forum.com/. >=20 Thanks for the help but your code is for me not understandable as beginner. Can you explain the how the operator and the reduce work.=20 Roelof = --_baf90789-a6bb-49ab-b546-970a22e829a8_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable


>=3B Date: Fri=2C 28 S= ep 2012 18:08:52 +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 Hi=2C
>=3B
&= gt=3B Roelof Wobben wrote in post #1077862:
>=3B >=3B I can declare = it as this def calculate (*arguments)
>=3B >=3B So i will be one ha= sh but then I have to figure out how to seperate the
>=3B >=3B optio= ns part and the numbers parts.
>=3B
>=3B Then why do you do it? = Just set the parameter list according to the
>=3B method call:
>= =3B
>=3B def calculate *numbers=2C options
>=3B ...
>=3B = end
>=3B
>=3B I'm also quite sure that you're not supposed to su= btract the numbers
>=3B from 0 but rather from each other. So calcula= te(1=2C 2=2C {}) should yield 1
>=3B - 2 =3D -1 and not 0 - 1 - 2 =3D= -3.
>=3B
>=3B In this case you must omit the "0" parameter for = "inject". And you
>=3B should use the short form:
>=3B
>= =3B
>=3B def calculate *numbers=2C options
>=3B operator =3D o= ptions[:add] ? :+ : :-
>=3B numbers.reduce operator
>=3B end>=3B
>=3B puts calculate 3=2C 5=2C add: true
>=3B puts calcul= ate 3=2C 5=2C add: false
>=3B puts calculate(3=2C 5=2C {})
>=3B <= br>>=3B --
>=3B Posted via http://www.ruby-forum.com/.
>=3B

Thanks for the help but your code is for me not understandable as= beginner.
Can you explain the how the operator and the reduce work.
Roelof

= --_baf90789-a6bb-49ab-b546-970a22e829a8_--