From: Roelof Wobben Date: 2012-09-28T17:37:32+09:00 Subject: Re: calcaulation with unknown numbers of numbers and options fail --_0a9472b3-4c63-4593-94c0-34ea72ddb8c8_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > Date: Fri=2C 28 Sep 2012 17:31:55 +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 10:18 AM=2C Roelof Wobben wrote: > > > > > >> Date: Fri=2C 28 Sep 2012 17:09:08 +0900 > >> From: jgabrielygalan@gmail.com > >> Subject: Re: calcaulation with unknown numbers of numbers and options = fail > >> To: ruby-talk@ruby-lang.org > > > >> > >> 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 specifiedNoMethodErrorundefine= d > >> > method > >> > `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?= ' for > >> > -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= are > >> > in > >> > the right variable. > >> > > >> > > >> > Anyone a tip how to solve this ? > >> > >> The options hash is the first parameter to your function=2C so pass it > >> as the first parameter: > >> > >> calculate({:add =3D> true}=2C 4=2C5) > >> > >> and also=2C you shouldn't check options =3D=3D "add"=2C but options[:a= dd] =3D=3D > >> true or just options[:add] > >> > >> Jesus. > >> > > > > Thanks=2C > > > > I can't change the function call because I try to learn ruby by a > > interactive website. >=20 > Then you have to modify how you declare the function. I can declare it as this def calculate (*arguments)=20 So i will be one hash but then I have to figure out how to seperate the opt= ions part and the numbers parts. back to irb how then arguments will look like and figure it out later. Roelof >=20 > > > > 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 > > array =3D empty. > > So the function defaults to "add" when nothing is entered. >=20 > Options is not an array=2C it's a hash. but in any case it's never going > to be equal to "add". It will contain an element or a key "add". >=20 > Jesus. >=20 = --_0a9472b3-4c63-4593-94c0-34ea72ddb8c8_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable


>=3B Date: Fri=2C 28 S= ep 2012 17:31:55 +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 10:18 AM=2C Roelof Wobben <=3Brwobben@hotmail.com>=3B wrote= :
>=3B >=3B
>=3B >=3B
>=3B >=3B>=3B Date: Fri=2C 28 = Sep 2012 17:09:08 +0900
>=3B >=3B>=3B From: jgabrielygalan@gmail.c= om
>=3B >=3B>=3B Subject: Re: calcaulation with unknown numbers of= numbers and options fail
>=3B >=3B>=3B To: ruby-talk@ruby-lang.or= g
>=3B >=3B
>=3B >=3B>=3B
>=3B >=3B>=3B On Fri=2C = Sep 28=2C 2012 at 9:40 AM=2C Roelof Wobben <=3Brwobben@hotmail.com>=3B<= br>>=3B >=3B>=3B wrote:
>=3B >=3B>=3B >=3B Hello=2C
>= =3B >=3B>=3B >=3B
>=3B >=3B>=3B >=3B I have this script:>=3B >=3B>=3B >=3B
>=3B >=3B>=3B >=3B def calculate (o= ptions =2C *numbers)
>=3B >=3B>=3B >=3B options <=3B<=3B "ad= d" if options.empty?
>=3B >=3B>=3B >=3B numbers.inject(0) { |sum= =2C number| sum + number } if options =3D=3D "add"
>=3B >=3B>=3B &= gt=3B numbers.inject() { |sum=2C number| sum - number } if options =3D=3D "= subtract"
>=3B >=3B>=3B >=3B end
>=3B >=3B>=3B >=3B>=3B >=3B>=3B >=3B But now I see this output in Rubymonks :
&g= t=3B >=3B>=3B >=3B
>=3B >=3B>=3B >=3B defaults to addtion = when no option is specifiedNoMethodErrorundefined
>=3B >=3B>=3B &g= t=3B method
>=3B >=3B>=3B >=3B `empty?' for 4:Fixnuminvoking cal= culate(4=2C 5=2C add: true) returns
>=3B >=3B>=3B >=3B 9NoMethod= Errorundefined method `empty?' for 4:Fixnuminvoking
>=3B >=3B>=3B = >=3B calculate(-10=2C
>=3B >=3B>=3B >=3B 2=2C 3=2C add: true) = returns -5NoMethodErrorundefined method `empty?' for
>=3B >=3B>=3B= >=3B -10:Fixnuminvoking calculate(0=2C 0=2C 0=2C 0=2C add: true) returns=
>=3B >=3B>=3B >=3B 0NoMethodErrorundefined method `empty?' for = 0:Fixnum
>=3B >=3B>=3B >=3B
>=3B >=3B>=3B >=3B I thin= k the problem is that I cannot take care that the right values are
>= =3B >=3B>=3B >=3B in
>=3B >=3B>=3B >=3B the right variable= .
>=3B >=3B>=3B >=3B
>=3B >=3B>=3B >=3B
>=3B >= =3B>=3B >=3B Anyone a tip how to solve this ?
>=3B >=3B>=3B>=3B >=3B>=3B The options hash is the first parameter to your functi= on=2C so pass it
>=3B >=3B>=3B as the first parameter:
>=3B &= gt=3B>=3B
>=3B >=3B>=3B calculate({:add =3D>=3B true}=2C 4=2C5= )
>=3B >=3B>=3B
>=3B >=3B>=3B and also=2C you shouldn't c= heck options =3D=3D "add"=2C but options[:add] =3D=3D
>=3B >=3B>= =3B true or just options[:add]
>=3B >=3B>=3B
>=3B >=3B>= =3B Jesus.
>=3B >=3B>=3B
>=3B >=3B
>=3B >=3B Thanks= =2C
>=3B >=3B
>=3B >=3B I can't change the function call beca= use I try to learn ruby by a
>=3B >=3B interactive website.
>= =3B
>=3B Then you have to modify how you declare the function.
I can declare it as this =3B 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.
back to irb how then arguments will look like and= figure it out later.

Roelof

>=3B
>=3B >=3B
>= =3B >=3B I don't understand one remark. You say I shouldn't check the opt= ions
>=3B >=3B =3D=3D"add" .
>=3B >=3B What I' trying to do i= s to add the word "add" to options if the options
>=3B >=3B array = =3D empty.
>=3B >=3B So the function defaults to "add" when nothing = is entered.
>=3B
>=3B Options is not an array=2C it's a hash. bu= t in any case it's never going
>=3B to be equal to "add". It will cont= ain an element or a key "add".
>=3B
>=3B Jesus.
>=3B
= --_0a9472b3-4c63-4593-94c0-34ea72ddb8c8_--