On Thu, Nov 20, 2014 at 9:46 AM,  <ang0123dev / gmail.com> wrote:
> What is the result of "puts sum(1, 2, 3).inspect"?

What is the sound of one hand clapping.  Sorry, your sentence just
triggered an association with me. :-)

> If the result is "[1, 2, 3]", then you should return sum variable at the end
> of sum method like below, but I recommend to use inject method.
>
> def sum (*numbers)
>   sum = 0
>   numbers.each do |n|
>     sum += n if n.is_a? Integer
>   end
>   sum
> end

I would omit the test because coercing takes care of type conversions.
If at all, I'd use #to_int but not the type test.
See http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

So that would still give

def sum(*numbers)
  inject(0) {|s,x| s+x}
end

or

def sum(*numbers)
  inject(0) {|s,x| s+x.to_int}
end

Roelof, the space before (1,2,3) is the issue.

Kind regards

robert

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