I have a method that takes a unlimited number of integers and adds
them all together .
So I did this :
def sum (*numbers)
numbers.each do |n| sum += n if n.is_a? Integer
end
end
but when I run it I see this error message : `block in sum': undefined method `+' for nil:NilClass (NoMethodError)
from `each'
from `sum'
from `
’
why do I get a nilClass there. As far as I know this is a normal
The 'sum' variable needs to be initialized or else it takes the value of
nil. So in the line 'sum += n', sum is nil. You can solve this by setting
sum = 0 at the beginning of the method.
···
On Wed, Nov 19, 2014 at 11:22 PM, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I have a method that takes a unlimited number of integers and adds them
all together .
So I did this :
def sum (*numbers)
numbers.each do |n|
sum += n if n.is_a? Integer
end
end
but when I run it I see this error message :
`block in sum': undefined method `+' for nil:NilClass (NoMethodError)
from `each'
from `sum'
from `
'
why do I get a nilClass there. As far as I know this is a normal way to
add things up.
That is solved,
But now I have this test :
Test.assert_equals(6, sum (1,2,3) )
And I see this error :
-e:10: syntax error, unexpected ( arg, expecting keyword_do or '{'
or ‘(’
Test.assert_equals(6, sum (1,2,3) )
Roelof
Jeremy Axelrod schreef op 20-11-2014 8:31:
The 'sum' variable needs to be initialized or else
it takes the value of nil. So in the line ‘sum += n’, sum is
nil. You can solve this by setting sum = 0 at the beginning of
the method.
Hello,
I have a method that takes a unlimited number of integers
and adds them all together .
So I did this :
> > def sum (*numbers)
> > numbers.each do |n| sum += n if n.is_a? Integer
> > end
> > end
> > but when I run it I see this error message : `block in sum': undefined method `+' for nil:NilClass (NoMethodError)
> > from `each'
> > from `sum'
> > from `
···
On Wed, Nov 19, 2014 at 11:22 PM, > Roelof Wobben <r.wobben@home.nl> > wrote:
’
why do I get a nilClass there. As far as I know this is a
Yup, you need sum to be defined. Use inject to do just that
···
On Thu, Nov 20, 2014 at 9:31 AM, Jeremy Axelrod <axelrod.jeremy@gmail.com> wrote:
The 'sum' variable needs to be initialized or else it takes the value of
nil. So in the line 'sum += n', sum is nil. You can solve this by setting
sum = 0 at the beginning of the method.
On Wed, Nov 19, 2014 at 11:22 PM, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I have a method that takes a unlimited number of integers and adds them
all together .
So I did this :
def sum (*numbers)
numbers.each do |n|
sum += n if n.is_a? Integer
end
end
but when I run it I see this error message :
`block in sum': undefined method `+' for nil:NilClass (NoMethodError)
from `each'
from `sum'
from `
'
why do I get a nilClass there. As far as I know this is a normal way to
add things up.
I would recommend the "inject" approach here. The reason why you are
getting nilClass and the error "undefined method `+' for nil:NilClass" is
because in the block, you are trying to call the "+" method on the "sum"
object which is essentially nil. A way to avoid this error is by defining a
variable "sum" outside the block and setting it to 0.
···
On Thu, Nov 20, 2014 at 1:01 PM, Jeremy Axelrod <axelrod.jeremy@gmail.com> wrote:
The 'sum' variable needs to be initialized or else it takes the value of
nil. So in the line 'sum += n', sum is nil. You can solve this by setting
sum = 0 at the beginning of the method.
On Wed, Nov 19, 2014 at 11:22 PM, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I have a method that takes a unlimited number of integers and adds them
all together .
So I did this :
def sum (*numbers)
numbers.each do |n|
sum += n if n.is_a? Integer
end
end
but when I run it I see this error message :
`block in sum': undefined method `+' for nil:NilClass (NoMethodError)
from `each'
from `sum'
from `
'
why do I get a nilClass there. As far as I know this is a normal way to
add things up.
On Thu, Nov 20, 2014 at 9:38 AM, Roelof Wobben <r.wobben@home.nl> wrote:
Thanks,
That is solved,
But now I have this test :
Test.assert_equals(6, sum (1,2,3) )
And I see this error :
-e:10: syntax error, unexpected ( arg, expecting keyword_do or '{' or '('
Test.assert_equals(6, sum (1,2,3) )
Roelof
Jeremy Axelrod schreef op 20-11-2014 8:31:
The 'sum' variable needs to be initialized or else it takes the value of
nil. So in the line 'sum += n', sum is nil. You can solve this by setting
sum = 0 at the beginning of the method.
On Wed, Nov 19, 2014 at 11:22 PM, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I have a method that takes a unlimited number of integers and adds them
all together .
So I did this :
def sum (*numbers)
numbers.each do |n|
sum += n if n.is_a? Integer
end
end
but when I run it I see this error message :
`block in sum': undefined method `+' for nil:NilClass (NoMethodError)
from `each'
from `sum'
from `
'
why do I get a nilClass there. As far as I know this is a normal way to
add things up.
Thanks,
I tried the inject way like this :
def sum (*numbers)
sum.inject(0) {|sum,nummer| sum + nummer}
end
with this test :
Test.assert_equals(6, sum(1,2,3) )
and now I see this error : -e:3: stack level too deep
On Thu, Nov 20, 2014 at 3:32 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Thu, Nov 20, 2014 at 4:09 PM, Roelof Wobben <r.wobben@home.nl> wrote:
> I had to do numbers.inject
Exactly.
> One other problem.
> The method schould reject everything which is not a integer.
> numbers.inject gives a error if you do sum (1,"a", 2)
Well, then it _does_ reject non numbers. Generally I probably would
not add a test to that method that simply skips non numbers. Let each
method do one thing properly. If you need filtering you can always do
the filtering before the call.
sum(*numbers.grep(Integer))
Btw, I would declare the interface of the method like this:
def sum(numbers)
i.e. use a single Enumerable. I think that is more flexible because
you can pass in an Enumerator which can be used to delegate the
filtering to the iteration (and not have to do it upfront and convert
to an Array like is needed with *numbers):
One other problem.
The method schould reject everything which is not a integer.
numbers.inject gives a error if you do sum (1,"a", 2)
Well, then it _does_ reject non numbers. Generally I probably would
not add a test to that method that simply skips non numbers. Let each
method do one thing properly. If you need filtering you can always do
the filtering before the call.
sum(*numbers.grep(Integer))
Btw, I would declare the interface of the method like this:
def sum(numbers)
i.e. use a single Enumerable. I think that is more flexible because
you can pass in an Enumerator which can be used to delegate the
filtering to the iteration (and not have to do it upfront and convert
to an Array like is needed with *numbers):