Ruby If,Else If命令语法

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值