我创建了以下内容(这是更多代码的一部分,但只有这一行有问题):
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语句?
ifRuby中的一行语句是"向后"完成的,可以这么说:
number_of_divisors += 1 if number_to_test % divisor == 0
Run Code Online (Sandbox Code Playgroud)
正如可以预料的那样,同样适用unless.
| 归档时间: |
|
| 查看次数: |
83 次 |
| 最近记录: |