ruby三元操作符_Ruby中的三元(有条件的)运算符是什么?

ruby三元操作符

The ternary (or conditional) operator will evaluate an expression and return one value if it's true, and another value if it's false. It's a bit like a shorthand, compact if statement.

三元运算符(或条件运算符)将对表达式求值,如果为true,则返回一个值,如果为false,则返回另一个值。 这有点像速记,紧凑的if语句。

Ruby's ternary operator has its uses but it's also a bit controversial.

Ruby的三元运算符有其用途,但也有争议。

三元运算符示例 ( Ternary Operator Example )

Let's look at this example:

让我们来看这个例子:

Here, the conditional operator is being used to select between two strings. The entire operator expression is everything including the conditional, question mark, two strings, and the colon. The general format of this expression is as follows: conditional ? true : false.

这里,条件运算符用于在两个字符串之间进行选择。 整个运算符表达式包括条件,问号,两个字符串和冒号。 该表达式的一般格式如下: 条件? true:错误

If the conditional expression is true, then the operator will evaluate as the true expression. Otherwise, it will evaluate as the false expression. In this example, it's in parentheses, so it doesn't interfere with the string concatenation operators surrounding it.

如果条件表达式为true,则运算符将评估为true表达式。 否则,它将评估为假表达式。 在此示例中,它在括号中,因此不会干扰其周围的字符串连接运算符。

To put this another way, the conditional operator is like an if statement. Remember that if statements in Ruby evaluate to the last value in the block that gets executed. So, you could rewrite the previous example like so:

换句话说,条件运算符就像一个if语句。 请记住, Ruby中的if语句求值是要执行的块中的最后一个值。 因此,您可以像这样重写前面的示例:

This code is functionally equivalent, and perhaps a bit easier to understand. If i is greater than 10, the if statement itself will evaluate to the string "greater than" or will evaluate to the string "less than or equal to." This is the same thing that the ternary operator is doing, only the ternary operator is more compact.

这段代码在功能上是等效的,也许更容易理解。 如果i大于10,则if语句本身将评估为字符串“大于”,或评估为字符串“小于或等于”。 这与三元运算符所做的相同,只是三元运算符更紧凑。

三元运算符的用途 ( Uses for the Ternary Operator )

So, what uses does the ternary operator have? It does have uses, but there aren't many, and you could get along fine without it.

那么,三元运算符有什么用? 它确实有用途,但用途不多,没有它,您可以相处得很好。

It's usually used to shoehorn in values where conditionals would be too bulky. It's also used in variable assignment to quickly select between two values. 

通常用在条件过多的值上。 它还在变量分配中用于在两个值之间快速选择。

Here are two typical use cases you'll see for the ternary operator:

这是三元运算符的两个典型用例:

You may have noticed that this looks quite un-Ruby. Complex expressions just don't belong on one line in Ruby - it's usually split up and easier to read. However, you will see this operator, and it can be used effectively without getting out of hand.

您可能已经注意到,这看起来非常不符合Ruby。 复杂表达式只是在Ruby中不属于同一行-通常会拆分并且更易于阅读。 但是,您将看到此运算符,并且可以有效使用它而不会失控。

One rule to follow is that if you're using this operator to select between two values with a simple conditional, it's OK to use. If you're doing something more complex, you should probably be using an if statement instead.

遵循的一条规则是,如果您使用此运算符通过简单的条件在两个值之间进行选择,则可以使用。 如果您要执行更复杂的操作,则可能应该使用if语句。

翻译自: https://www.thoughtco.com/ternary-or-conditional-operator-2907827

ruby三元操作符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值