Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ruby's "syntax error, unexpected `end', expecting end-of-input"

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 doend or {}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading