I have an exercise of accumulating proc parameters, but the "syntax error, unexpected `end’, expecting end-of-input" message appears during the actual manipulation, I can’t find where there is an extra or a missing "end", please tell me, thank you.
def total2(from, to, &block)
result = 0
from.upto(to) |num|
if block
result +=
block.call(num)
else
result += num
end
end
return result
end
p total2(1, 10)
p total2(1, 10){|num| num ** 2}
>Solution :
do is missed
from.upto(to) do |num|
# block body
end
https://ruby-doc.org/core/doc/syntax/calling_methods_rdoc.html#label-Block+Argument
The block argument is always last when sending a message to a method. A block is sent to a method using
do…endor{…}