作為一個表達式¶
if
的值是其每個分支中找到的最後一個表達式的值
a = if 2 > 1
3
else
4
end
a # => 3
如果 if
分支為空,或缺少,則視為其中包含 nil
if 1 > 2
3
end
# The above is the same as:
if 1 > 2
3
else
nil
end
# Another example:
if 1 > 2
else
3
end
# The above is the same as:
if 1 > 2
nil
else
3
end