The Ruby language has a very simple control structure that is easy to read and follow.
Ruby语言的控件结构非常简单,易于阅读和遵循。
If syntax
如果语法
if var == 10 print “Variable is 10” end
如果var == 10,则显示“ Variable is 10”
If Else Syntax
如果其他语法
if var == 10 print “Variable is 10” else print “Variable is something else” end
如果var == 10,则显示“ Variable is 10”,否则,显示“ Variable is another”
If Else If Syntax
如果其他如果语法
Here’s the key difference between Ruby and most other languages. Note that “else if” is actually spelled “elsif” without the e.
这是Ruby和大多数其他语言之间的主要区别。 请注意,“ else if”实际上是不带e的“ elsif”。
if var == 10 print “Variable is 10” elsif var == “20” print “Variable is 20” else print “Variable is something else” end
如果var == 10,则打印“ Variable is 10”;如果var ==“ 20”,则打印“ Variable is 20”,否则,打印“ Variable is something”。
Ternary (shortened if statement) Syntax
三元(if语句缩短)语法
Ternary syntax is the same in Ruby as most languages. The following sample will print “The variable is 10” if var is equal to 10. Otherwise it will print “The variable is Not 10”.
在Ruby中,三元语法与大多数语言相同。 如果var等于10,以下示例将显示“变量为10”。否则,将显示“变量为非10”。
print “The variable is ” + (var == 10 ? “10” : “Not 10”)
打印“变量为” +(var == 10?“ 10”:“不是10”)
翻译自: https://www.howtogeek.com/howto/programming/ruby/ruby-if-else-if-command-syntax/
|