What's wrong with this code?

Can anybody troubleshoot my code? Where am I going wrong, please? Thanks, C

def puts_1_to_10
(1…10).each { |i| if i / 2 == 0 puts i }
end

(ruby):1: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ‘;’ or ‘\n’
(1…10).each { |i| if i / 2 == 0 puts i }

I am assuming your intent is to print even numbers. If so, instead of i / 2 == 0, it should be i % 2 == 0

If you want to use curly braces and write a one-line if statement, then here are 3 possible ways to do so:

(1...10).each { |i| puts i if i % 2 == 0}

(1...10).each { |i| if i % 2 == 0 then puts i end}

(1...10).each { |i| if i % 2 == 0; puts i end}

1 Like