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 method `call' for 4:Fixnum Can anyone give me a tip where I did go wrong ? Roelof

on 2014-06-20 22:22

on 2014-06-20 22:26

On Jun 20, 2014, at 13:22, Roelof Wobben <r.wobben@home.nl> 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?

on 2014-06-20 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 * 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 Davis <ryand-ruby@zenspider.com>

on 2014-06-20 22:44

Ryan Davis 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

on 2014-06-20 22:47

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

on 2014-06-20 23:15

<html> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> </head> <body text="#000000" bgcolor="#FFFFFF"> <div class="moz-cite-prefix">I have now this ; <br> <br> def add (*numbers)<br> ?? numbers.inject(0) { |sum, number| sum + number }<br> end<br> <br> def subtract(*numbers)<br> ???? numbers.inject() { |sum, number| sum - number }<br> end<br> <br> def calculate(options = { :add => true,:substract => false}, *numbers)<br> ?? add(*numbers) if options[:add] == true<br> ?? subtract(*numbers) if options[:substract] == true<br> end<br> <br> <br> But it gives this error : <br> <br> <dl class="window" id="magic_block-output-3899" style="display: block;"> <dt class="fail">defaults to addtion when no option is specified</dt> <dd class="fail">TypeError</dd> <dd class="fail">can't convert Symbol into Integer</dd> <dt class="fail">invoking calculate(4, 5, add: true) returns 9</dt> <dd class="fail">TypeError</dd> <dd class="fail">can't convert Symbol into Integer</dd> </dl> <p><br> Roelof<br> <br> </p> <br> <br> <br> <br> Roelof Wobben schreef op 20-6-2014 22:47:<br> </div> <blockquote cite="mid:53A49DD3.4090108@home.nl" type="cite">Ryan Cook schreef op 20-6-2014 22:37: <br> <blockquote type="cite">Hi Roelof, <br> <br> As Ryan mentioned, you have a couple of issues... <br> <br> * There is a naming conflict with "add" the method and "add" the boolean function argument <br> <br> * 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 <br> <br> </blockquote> <br> If I look at the test i can contain add = true or substract = true <br> <br> <br> <blockquote type="cite">* You cannot easily use "call" with normal ruby methods. Simply add(1,2,4) or subtract(1,2,4) are acceptable <br> <br> </blockquote> <br> what Is then the best way to do calculate (1,2, add=true)?? ? <br> <br> <blockquote type="cite">* 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. <br> </blockquote> <br> add could start with 0 . Substract schould start with the first number I think <br> <br> <blockquote type="cite"> <br> I hope this is helpful! <br> <br> Ryan <br> <br> <br> On Fri, Jun 20, 2014 at 2:25 PM, Ryan Davis <<a class="moz-txt-link-abbreviated" href="mailto:ryand-ruby@zenspider.com">ryand-ruby@zenspider.com</a> <a class="moz-txt-link-rfc2396E" href="mailto:ryand-ruby@zenspider.com"><mailto:ryand-ruby@zenspider.com></a>> wrote: <br> <br> <br> ?????? On Jun 20, 2014, at 13:22, Roelof Wobben <<a class="moz-txt-link-abbreviated" href="mailto:r.wobben@home.nl">r.wobben@home.nl</a> <br> ?????? <a class="moz-txt-link-rfc2396E" href="mailto:r.wobben@home.nl"><mailto:r.wobben@home.nl></a>> wrote: <br> <br> ?????? > def calculate(add = true, *numbers) <br> ?????? >?? if add <br> ?????? >?????? add.call(*numbers) <br> ?????? >?? else <br> ?????? >?????? subtract.call(*numbers) <br> ?????? >?? end <br> ?????? > end <br> <br> ?????? What is that supposed to do? <br> <br> ?????? `add` is a local variable that defaults to true, so you're calling <br> ?????? true.call(*numbers)? What should that do? <br> <br> ?????? `subtract` isn't a local variable. What should it do when you send <br> ?????? `call` to it? <br> <br> <br> <br> <br> --??<br> Ryan Cook <br> 720.319.7660 <br> </blockquote> <br> </blockquote> <br> </body> </html>

on 2014-06-21 00:13

-----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 ruby-talk-request@ruby-lang.org. 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-lis... 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 Fowler rylee@rylee.me -----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 2014-06-21 01:04

On Fri, Jun 20, 2014 at 11:14 PM, Roelof Wobben <r.wobben@home.nl> 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.

on 2014-06-21 08:52

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