多线如果是红宝石

use*_*204 1 ruby if-statement block

我知道其他地方已经触及了这些问题,但我对if elseruby中多行(块?)语句的正确语法略感不安.

举个例子:

if condition then
  do something
  do somethingelse
  do yetanotherthing
  done
else
  do acompletelyunrelatedthing
done
Run Code Online (Sandbox Code Playgroud)

我知道then如果使用多行,则需要声明,但是doneelse必要之前是否需要?这似乎会脱离if...else上下文.当我这样做时,done我得到:

syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'

当我不包括它时,我得到:

syntax error, unexpected keyword_else, expecting keyword_end

Doo*_*nob 5

嗯...... doneRuby中没有关键字.这是正确的语法:

if condition
  # do stuff
else
  # do other stuff
end
Run Code Online (Sandbox Code Playgroud)

then不被需要的关键字.