跳至內容

除非 (unless)

unless 的條件為假值時,會執行 then 分支,若有 else 分支,則會執行它,否則會繼續往下執行。也就是說,它的行為與 if 相反。

unless some_condition
  expression_when_falsey
else
  expression_when_truthy
end

# The above is the same as:
if some_condition
  expression_when_truthy
else
  expression_when_falsey
end

# Can also be written as a suffix
close_door unless door_closed?