I created the following (this is part of more code but only this line is at issue):
if number_to_test % divisor == 0 then number_of_divisors+= 1 end
I wanted to shorten it up with
if number_to_test % divisor == 0 { number_of_divisors+= 1 }
but I get
syntax error, unexpected '{', expecting keyword_then or ';' or '\n'
if number_to_test % divisor == 0 { number_of_divisors+= 1 }
^
syntax error, unexpected '}', expecting keyword_end
I thought I could change the long form into a {}
Can you tell me the right syntax (if possible) ?
Maybe I’m just thinking of do end blocks rather than if statements?
One line
ifstatements in Ruby are done “backwards”, so to speak:As might be expected, the same works with
unlessas well.