From: Roelof Wobben Date: 2012-09-28T17:18:47+09:00 Subject: Re: calcaulation with unknown numbers of numbers and options fail --_eab9cf56-c2af-4444-b66d-23cb8e6e96c9_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > Date: Fri=2C 28 Sep 2012 17:09:08 +0900 > From: jgabrielygalan@gmail.com > Subject: Re: calcaulation with unknown numbers of numbers and options fai= l > To: ruby-talk@ruby-lang.org >=20 > On Fri=2C Sep 28=2C 2012 at 9:40 AM=2C Roelof Wobben wrote: > > Hello=2C > > > > I have this script: > > > > def calculate (options =2C *numbers) > > options << "add" if options.empty? > > numbers.inject(0) { |sum=2C number| sum + number } if options =3D=3D = "add" > > numbers.inject() { |sum=2C number| sum - number } if options =3D=3D "= subtract" > > end > > > > But now I see this output in Rubymonks : > > > > defaults to addtion when no option is specifiedNoMethodErrorundefined m= ethod > > `empty?' for 4:Fixnuminvoking calculate(4=2C 5=2C add: true) returns > > 9NoMethodErrorundefined method `empty?' for 4:Fixnuminvoking calculate(= -10=2C > > 2=2C 3=2C add: true) returns -5NoMethodErrorundefined method `empty?' f= or > > -10:Fixnuminvoking calculate(0=2C 0=2C 0=2C 0=2C add: true) returns > > 0NoMethodErrorundefined method `empty?' for 0:Fixnum > > > > I think the problem is that I cannot take care that the right values ar= e in > > the right variable. > > > > > > Anyone a tip how to solve this ? >=20 > The options hash is the first parameter to your function=2C so pass it > as the first parameter: >=20 > calculate({:add =3D> true}=2C 4=2C5) >=20 > and also=2C you shouldn't check options =3D=3D "add"=2C but options[:add]= =3D=3D > true or just options[:add] >=20 > Jesus. >=20 Thanks=2C =20 I can't change the function call because I try to learn ruby by a interact= ive website. I don't understand one remark. You say I shouldn't check the options =3D= =3D"add" . What I' trying to do is to add the word "add" to options if the options arr= ay =3D empty. So the function defaults to "add" when nothing is entered. Roelof = --_eab9cf56-c2af-4444-b66d-23cb8e6e96c9_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable


>=3B Date: Fri=2C 28 S= ep 2012 17:09:08 +0900
>=3B From: jgabrielygalan@gmail.com
>=3B S= ubject: Re: calcaulation with unknown numbers of numbers and options fail>=3B To: ruby-talk@ruby-lang.org
>=3B
>=3B On Fri=2C Sep 28= =2C 2012 at 9:40 AM=2C Roelof Wobben <=3Brwobben@hotmail.com>=3B wrote:=
>=3B >=3B Hello=2C
>=3B >=3B
>=3B >=3B I have this sc= ript:
>=3B >=3B
>=3B >=3B def calculate (options =2C *numbers= )
>=3B >=3B options <=3B<=3B "add" if options.empty?
>=3B= >=3B numbers.inject(0) { |sum=2C number| sum + number } if options =3D= =3D "add"
>=3B >=3B numbers.inject() { |sum=2C number| sum - numbe= r } if options =3D=3D "subtract"
>=3B >=3B end
>=3B >=3B
&= gt=3B >=3B But now I see this output in Rubymonks :
>=3B >=3B
&= gt=3B >=3B defaults to addtion when no option is specifiedNoMethodErrorun= defined method
>=3B >=3B `empty?' for 4:Fixnuminvoking calculate(4= =2C 5=2C add: true) returns
>=3B >=3B 9NoMethodErrorundefined method= `empty?' for 4:Fixnuminvoking calculate(-10=2C
>=3B >=3B 2=2C 3=2C = add: true) returns -5NoMethodErrorundefined method `empty?' for
>=3B &= gt=3B -10:Fixnuminvoking calculate(0=2C 0=2C 0=2C 0=2C add: true) returns>=3B >=3B 0NoMethodErrorundefined method `empty?' for 0:Fixnum
>= =3B >=3B
>=3B >=3B I think the problem is that I cannot take care = that the right values are in
>=3B >=3B the right variable.
>=3B= >=3B
>=3B >=3B
>=3B >=3B Anyone a tip how to solve this ?<= br>>=3B
>=3B The options hash is the first parameter to your functi= on=2C so pass it
>=3B as the first parameter:
>=3B
>=3B cal= culate({:add =3D>=3B true}=2C 4=2C5)
>=3B
>=3B and also=2C you= shouldn't check options =3D=3D "add"=2C but options[:add] =3D=3D
>=3B= true or just options[:add]
>=3B
>=3B Jesus.
>=3B

T= hanks=2C
 =3B
I can't change the function call because I try to l= earn =3B ruby by a interactive website.

I don't understand one r= emark. You say I shouldn't check the options =3B =3D=3D"add" .
What = I' trying to do is to add the word "add" to options if the options array = =3D empty.
So the function defaults to "add" when nothing is entered.
Roelof

= --_eab9cf56-c2af-4444-b66d-23cb8e6e96c9_--