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
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 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
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> wrote:
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?
--
Ryan Cook
720.319.7660
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-lists/manual-instructions/
and https://www.ruby-lang.org/en/community/mailing-lists/.
···
On 6/20/2014 16:25, Andrew Kelley wrote:
Unsubscribe
On Jun 20, 2014, at 1:22 PM, Roelof Wobben <r.wobben@home.nl> >> wrote:
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
- --
Rylee Fowler
rylee@rylee.me
Ryan Davis schreef op 20-6-2014 22:25:
···
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?
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:
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 Davis <ryand-ruby@zenspider.com > <mailto:ryand-ruby@zenspider.com>> wrote:
On Jun 20, 2014, at 13:22, Roelof Wobben <r.wobben@home.nl > <mailto: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?
--
Ryan Cook
720.319.7660
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 Wobben 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 Davis > > <ryand-ruby@zenspider.com > > <mailto:ryand-ruby@zenspider.com> > wrote:
On Jun 20, 2014, at 13:22, Roelof Wobben > > <r.wobben@home.nl > > > > > > <mailto: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?
--
Ryan Cook
720.319.7660
Take a look at the order of the arguments when calling calculate.
Jesus.
···
On Fri, Jun 20, 2014 at 11:14 PM, Roelof Wobben <r.wobben@home.nl> wrote:
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
Jesús Gabriel y Galán schreef op 21-6-2014 1:04:
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
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
···
On Fri, Jun 20, 2014 at 11:14 PM, Roelof Wobben <r.wobben@home.nl> wrote: