Ruby monk problem

Hello,

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

Can anyone give me a tip where I did go wrong ?

Roelof

Unsubscribe

On Jun 20, 2014, at 13:22, Roelof W. [email protected] wrote:

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

What is that supposed to do?

add is a local variable that defaults to true, so you’re calling
true.call(*numbers)? What should that do?

subtract isn’t a local variable. What should it do when you send
call to it?

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

  • 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.

I hope this is helpful!

Ryan

On Fri, Jun 20, 2014 at 2:25 PM, Ryan D. [email protected]

Ryan D. schreef op 20-6-2014 22:25:

add is a local variable that defaults to true, so you’re calling
true.call(*numbers)? What should that do?

subtract isn’t a local variable. What should it do when you send call to it?

add or substract numbers like this :

add( 1,2,3)
substract(1,2,3)
calculate(4, 5, add: true)
calculate(4, 5, subtract: true)

Roelof

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

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

I have now this ;

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


I hope this is helpful!

Ryan


On Fri, Jun 20, 2014 at 2:25 PM, Ryan D. <[email protected] <mailto:[email protected]>> wrote:


?????? On Jun 20, 2014, at 13:22, Roelof W. <[email protected]
?????? <mailto:[email protected]>> wrote:

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

?????? What is that supposed to do?

?????? `add` is a local variable that defaults to true, so you're calling
?????? true.call(*numbers)? What should that do?

?????? `subtract` isn't a local variable. What should it do when you send
?????? `call` to it?




--??
Ryan Cook
720.319.7660


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

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:

  1. Point your web browser at
    https://www.ruby-lang.org/en/community/mailing-lists/ and unsubscribe
    via the simple web form at the bottom of the page (change the action
    to “unsubscribe”), or

  2. 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.

Instructions sourced from
https://www.ruby-lang.org/en/community/mailing-lists/manual-instructions/
and https://www.ruby-lang.org/en/community/mailing-lists/.

On 6/20/2014 16:25, Andrew Kelley wrote:

So I tried this :
but then I see this errors :

Roelof


Rylee F.
[email protected]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBCgAGBQJTpLH4AAoJEAWmNCH2N+MzHHgH/2Lb56TMExNq8+rLAgVjq1km
w9ngrchyQjxPfTMbm5orkIVHrRTm74vYbG0l0jsIwsVo4k66WcrcE6xKWQSS0u5c
0BaiwYqEn0uOYKnK+nuMn40iKLS2yxAOzbL+9ZWLFW45dxY+I0zR65tZKcQ3CrqQ
GCcNrbnzGye+uP9F7xpG6JOEpqr4yGN1LBlmCExggPZG+ftSUZc4eCV5x/8DkjT6
cgFrycNGqnKw4uoq6WLVpArrvSw+sSAwGBVDxqtRgSx/942d/lvVm6vYMs+8DxBg
ZOXZjQHPlsmN571ghCO4XAGsWeA9zYrOICMbojpIKPMs5R/chqOzc6Yg0GSP2Ws=
=wuOP
-----END PGP SIGNATURE-----

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.

Jesus.

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

I see this error :

class: SyntaxError message:
/data/EvalServer/releases/20140425044352/app/models/eval_spec_runner.rb:13:
syntax error, unexpected ‘=’, expecting ‘)’ def calculate(*numbers,
options = { :add => true,:substract => false}) ^
/data/EvalServer/releases/20140425044352/app/models/eval_spec_runner.rb:13:
syntax error, unexpected ‘)’, expecting $end backtrace: RubyMonk:18:in
`eval’

So I think and hope I can solve it by looking if *numbers has add or
subtract included and use select to find only the numbers.

Roelof

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs