I have to make three defenitions add, subtract and calculate with has to
work with a unknown number of numbers.

So I tried this :

def add (*numbers)
numbers.inject(0) { |sum, number| sum + number }
end

def subtract(*numbers)
numbers.inject() { |sum, number| sum - number }
end

def calculate(add = true, *numbers)
if add
add.call(*numbers)
else
subtract.call(*numbers)
end
end

but then I see this errors :

defaults to addtion when no option is specified
NoMethodError
undefined method call' for 4:Fixnum invoking calculate(4, 5, add: true) returns 9 NoMethodError undefined methodcall’ for 4:Fixnum

There is a naming conflict with “add” the method and “add” the boolean
function argument

It looks like you may be trying to use Ruby 2 keyword arguments in the
“calculate” method with the “add” keyword? In actuality, you are naming
an
argument “add” with a default value of true

You cannot easily use “call” with normal ruby methods. Simply
add(1,2,4)
or subtract(1,2,4) are acceptable

Your inject block in “subtract” does not start with any number. This
is
actually ambiguous. Should it start with 0 or the first argument?
Technically, the same question applies to the “add” method.

If I look at the test i can contain add = true or substract = true

You cannot easily use “call” with normal ruby methods. Simply
add(1,2,4) or subtract(1,2,4) are acceptable

what Is then the best way to do calculate (1,2, add=true) ?

Your inject block in “subtract” does not start with any number. This
is actually ambiguous. Should it start with 0 or the first argument?
Technically, the same question applies to the “add” method.

add could start with 0 . Substract schould start with the first number I
think

def add (*numbers)
?? numbers.inject(0) { |sum, number| sum + number }
end

def subtract(*numbers)
???? numbers.inject() { |sum, number| sum - number }
end

def calculate(options = { :add => true,:substract => false},
*numbers)
?? add(*numbers) if options[:add] == true
?? subtract(*numbers) if options[:substract] == true
end

But it gives this error :

defaults to addtion when no option is
specified

TypeError

can't convert Symbol into Integer

invoking calculate(4, 5, add: true) returns
9

TypeError

can't convert Symbol into Integer

Roelof

Roelof W. schreef op 20-6-2014 22:47:

Ryan
Cook schreef op 20-6-2014 22:37:

Hi Roelof,

As Ryan mentioned, you have a couple of issues...

* There is a naming conflict with "add" the method and "add" the
boolean function argument

* It looks like you may be trying to use Ruby 2 keyword
arguments in the "calculate" method with the "add" keyword? In
actuality, you are naming an argument "add" with a default value
of true

If I look at the test i can contain add = true or substract = true

* You cannot easily use "call" with normal
ruby methods. Simply add(1,2,4) or subtract(1,2,4) are
acceptable

what Is then the best way to do calculate (1,2, add=true)?? ?

* Your inject block in "subtract" does not
start with any number. This is actually ambiguous. Should it
start with 0 or the first argument? Technically, the same
question applies to the "add" method.

add could start with 0 . Substract schould start with the first
number I think

Hello! It seems like you’ve tried to unsubscribe from the mailing list
you’re on (Ruby-Talk). There’s two simple ways to accomplish this task:

Point your web browser at Mailing Lists and unsubscribe
via the simple web form at the bottom of the page (change the action
to “unsubscribe”), or

Send an e-mail to the controller address for this mailing list
(Ruby-Talk), which is located at [email protected]. The
email should have the subject “unsubscribe” and the body “unsubscribe”
(no quotes). Please be sure to send this as a plain-text e-mail, as
HTML e-mails have known issues with interacting with controller e-mails.

On Fri, Jun 20, 2014 at 11:14 PM, Roelof W. [email protected]
wrote:

def calculate(options = { :add => true,:substract => false}, *numbers)
TypeError can’t convert Symbol into Integer
Take a look at the order of the arguments when calling calculate.

Jes??s Gabriel y Gal??n schreef op 21-6-2014 1:04:

invoking calculate(4, 5, add: true) returns 9
TypeError can’t convert Symbol into Integer
Take a look at the order of the arguments when calling calculate.

Jesus.

When I do that :

def calculate(*numbers, options = { :add => true,:substract => false})
add(*numbers) if options[:add] == true
subtract(*numbers) if options[:substract] == true
end