antonyCas antonyCas - 4 months ago 9
Ruby Question

An integer I pass as an argument becomes a method

I am trying to write a function. Here is the code.

def get_sum(a,b)
if a == b do
return a
end
else
total = 0
for num in a...b
total += num
end
return total
end
end


I get this error:

undefined method `b' for main:Object (NoMethodError)


Can anyone tell me why I get this?

Answer

No do for ifs

if a == b
  return a
end

Note that you can do the same thing so

def get_sum(a, b)
  return a if a == b
  (a...b).inject(:+)
end
Comments