Why can I here sum numbers

Another valid option would be

def sum(*numbers)
numbers.reduce(&:+)
end

On Thu, Nov 20, 2014 at 3:32 PM, Robert K.
[email protected]
wrote:

Well, then it does reject non numbers. Generally I probably would
i.e. use a single Enumerable. I think that is more flexible because

robert


[guy, jim].each {|him| remember.him do |as, often| as.you_can - without
end}
http://blog.rubybestpractices.com/


George D.
Software Engineer

+44 (0)333 240 2222

[image: Rentify]
6-8 Long Lane, London EC1A 9HF
www.rentify.com

On Thu, Nov 20, 2014 at 4:37 PM, George D. [email protected]
wrote:

Another valid option would be

def sum(*numbers)
numbers.reduce(&:+)
end

$ ruby <<CODE

def sum(*numbers)
numbers.reduce(&:+)
end
p sum()
CODE
nil

Same store: empty list is not properly dealt with.

Cheers

robert

I found another answer :

def sum (*numbers)
???? numbers.inject(0) {|som,nummer| (nummer.is_a? Integer) ? sum + nummer : sum }
end

but when I do this test :

Test.assert_equals(4, sum(1,"a", 3) )

then I see this answer :

Expected: 3, instead got: 4

but how on earth is 3 expected??

Roelof




Robert K. schreef op 20-11-2014 17:25:


On Thu, Nov 20, 2014 at 4:37 PM, George D. <[email protected]> wrote:
Another valid option would be

def sum(*numbers)
?? numbers.reduce(&:+)
end


$ ruby <<CODE
> def sum(*numbers)
> ?? numbers.reduce(&:+)
> end
> p sum()
> CODE
nil

Same store: empty list is not properly dealt with.

Cheers

robert

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/

On 2014-Nov-20, at 13:04 , Roelof W. [email protected] wrote:

then I see this answer :

Expected: 3, instead got: 4

but how on earth is 3 expected

Roelof

I’m guessing that you didn’t paste that answer in, but typed it.

Notice that your block variables are |som,nummer| but then you use sum
in the rest of the block.

sum
#=> 0

so your loop is:
|som=0, nummer=1| #=> sum + nummer == 0 + 1
|som=1, nummer=“a”| #=> sum == 0 (Note, sum is sum() not som)
|som=0, nummer=3| #=> sum + nummer == 0 + 3 == 3

3

-Rob

Rob B. schreef op 20-11-2014 19:24:

On 2014-Nov-20, at 13:04 , Roelof W. <[email protected]> wrote:

I found another answer :

def sum (*numbers)
   numbers.inject(0) {|som,nummer| (nummer.is_a? Integer) ? sum + nummer : sum }
end

but when I do this test :

Test.assert_equals(4, sum(1,"a", 3) )

then I see this answer :

Expected: 3, instead got: 4

but how on earth is 3 expected 

Roelof

I'm guessing that you didn't paste that answer in, but typed it.

Notice that your block variables are |som,nummer| but then you use sum in the rest of the block.

> sum
#=> 0

so your loop is:
|som=0, nummer=1| #=> sum + nummer == 0 + 1
|som=1, nummer="a"| #=> sum == 0 (Note, sum is sum() not som)
|som=0, nummer=3| #=> sum + nummer == 0 + 3 == 3

3

-Rob

Robert K. schreef op 20-11-2014 17:25:


On Thu, Nov 20, 2014 at 4:37 PM, George D. <[email protected]> wrote:
Another valid option would be

def sum(*numbers)
  numbers.reduce(&:+)
end


$ ruby <<CODE
> def sum(*numbers)
>   numbers.reduce(&:+)
> end
> p sum()
> CODE
nil

Same store: empty list is not properly dealt with.

Cheers

robert

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/



Thanks, finally I got it working.

Roelof