ruby错误 - 我可以使用{...}将此块转换为单行吗?

jun*_*nky 1 ruby

我创建了以下内容(这是更多代码的一部分,但只有这一行有问题):

if number_to_test % divisor == 0 then number_of_divisors+= 1 end
Run Code Online (Sandbox Code Playgroud)

我想缩短它

if number_to_test % divisor == 0 { number_of_divisors+= 1 }
Run Code Online (Sandbox Code Playgroud)

但我明白了

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
Run Code Online (Sandbox Code Playgroud)

我以为我可以将长格式改为{}
你能告诉我正确的语法(如果可能的话)吗?
也许我只是想做结束块而不是if语句?

And*_*all 5

ifRuby中的一行语句是"向后"完成的,可以这么说:

number_of_divisors += 1 if number_to_test % divisor == 0
Run Code Online (Sandbox Code Playgroud)

正如可以预料的那样,同样适用unless.