From: Roelof Wobben Date: 2012-09-28T16:40:37+09:00 Subject: calcaulation with unknown numbers of numbers and options fail --_e72f6c5d-2d3d-4f5c-bf4a-e4866ff4401c_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hello=2C=20 I have this script: def calculate (options =2C *numbers)=20 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 "subt= ract" end But now I see this output in Rubymonks : defaults to addtion when no option is specifiedNoMethodErrorundefined metho= d `empty?' for 4:Fixnuminvoking calculate(4=2C 5=2C add: true) returns 9NoM= ethodErrorundefined method `empty?' for 4:Fixnuminvoking calculate(-10=2C 2= =2C 3=2C add: true) returns -5NoMethodErrorundefined method `empty?' for -1= 0:Fixnuminvoking calculate(0=2C 0=2C 0=2C 0=2C add: true) returns 0NoMethod= Errorundefined 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 ? Roelof = --_e72f6c5d-2d3d-4f5c-bf4a-e4866ff4401c_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hello=2C

I have this script:

def calculate (options =2C *num= bers)
 =3B options <=3B<=3B "add" if options.empty?
 =3B= numbers.inject(0) { |sum=2C number| sum + number } if options =3D=3D "add"=
 =3B 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 specified
NoM= ethodError
undefined method `empty?' for 4:Fixnum
invoking calculate(4=2C 5=2C add: true) returns 9
=
NoMethodError
undefined method `e= mpty?' for 4:Fixnum
invoking calculate(-10=2C 2=2C 3= =2C add: true) returns -5
NoMethodError
undefined method `empty?' for -10:Fixnum
= invoking calculate(0=2C 0=2C 0=2C 0=2C add: true) returns 0
NoMethodError
undefined method `empty?' fo= r 0:Fixnum


I think the problem is that I cannot take care = that the right values are in the right variable.


Anyone a tip ho= w to solve this ?


Roelof



= --_e72f6c5d-2d3d-4f5c-bf4a-e4866ff4401c_--