我知道这个问题在其他地方也有涉及,但我对ruby中多行(块?) if else语句的正确语法有点困惑。
举个例子:
if condition then
do something
do somethingelse
do yetanotherthing
done
else
do acompletelyunrelatedthing
done我知道如果使用多行,then语句是必需的,但是在else之前的done是必要的吗?这似乎会打破if...else的上下文。当我包含这个done时,我会得到:
syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'
当我不包含它时,我会得到:
syntax error, unexpected keyword_else, expecting keyword_end
发布于 2013-04-22 07:29:39
仅当您需要将整个操作放在一行中时才使用then关键字(以便将条件与要在true情况下执行的操作分开):
if condition then do_something else do_something_different end如果你不想用一行代码(通常你也不想),那么语法就像门把手的答案一样。
发布于 2013-04-22 06:44:15
嗯..。Ruby中没有done关键字。下面是正确的语法:
if condition
# do stuff
else
# do other stuff
endthen关键字也不是必需的。
https://stackoverflow.com/questions/16136983
复制相似问题