Ruby:比较级之间有什么区别:“||"?和“或" [英] Ruby: what is the difference between the comparatives: "||" and "or"

查看:75
本文介绍了Ruby:比较级之间有什么区别:“||"?和“或"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Ruby:||之间的区别和‘或’

使用红宝石

||

or 

是非常常见的做法,因此了解两者之间的区别很重要,因为不幸的是我不确定.

are very common practices which makes it important to know the difference between the two as unfortunately I am not sure.

首先我的问题是以下假设是否正确:

First of all my question is if the following assumption is correct:

EX1:

if @variable_1 || @variable_2 || @variable_3 
  do something 
else
  do nothing
end

EX2:

if @variable_1 or @variable_2 or @variable_3
  do something 
else
  do nothing
end

因此,在第一个示例中,如果任何变量为 false,则它将执行什么都不做"

So in the first example if any variable is false then it will execute "do nothing"

然而,对于第二个例子,所有变量都被检查,如果有一个为真,那么它将执行做某事".

However, for the second example all variables are checked and if one is true then it will execute "do something".

总结使用||"如果您有一个需要检查的变量列表,并且其中一个返回 false,则标志会上升.使用带有变量列表的第二个示例,其中只有一个变量需要为真才能继续执行所需的代码.

In summary use "||" if you have a list of variables that need to be checked and if one of them returns false then a flag goes up. Use the second example with a list of variables where only one needs to be true in order to continue executing the desired code.

这些假设是否正确?

推荐答案

或者第二个例子所有的变量都被检查,如果一个是真的那么它会执行做某事".

or the second example all variables are checked and if one is true then it will execute "do something".

这是一句假话.

因此您的假设是不正确的.

As a result your assumptions are not correct.

or|| 都做同样的事情.

Both or and || do the same thing.

主要区别在于 or 的优先级低于 ||.所以你应该注意更复杂的评估:

The main difference is that or has lower precedence than ||. So you should pay attention to more complex evaluations:

# Simple cases are not confusing
false || true # true
false or true # true

# This is more complex
a = false || true # a=true
a = false or true # a=false

# Also similarly as 1 + 2*3 returns 7, the following returns true:
false or false||true # true
# BUT! THIS IS IMPORTANT!
a = false or false||true   # a=false
a = (false or false||true) # a=true

这是运算符优先级的列表.

因此,如果您使用包含以下任何运算符的表达式,就会发现真正的区别:

So the real difference will be noticed if you use the expression that includes any of the following operators:

  • .. ... - 范围(包含和不包含)
  • <代码>?: - 三元 if-then-else
  • = %= {/= -= += |= &= >>= <<= *= &&= ||= **= - 赋值
  • defined? - 检查是否定义了指定的符号
  • not - 逻辑否定
  • and - 逻辑组合
  • .. ... - Range (inclusive and exclusive)
  • ? : - Ternary if-then-else
  • = %= { /= -= += |= &= >>= <<= *= &&= ||= **= - Assignment
  • defined? - Check if specified symbol defined
  • not - Logical negation
  • and - Logical composition

可能还有其他人.

+* 之间的区别你可以理解:||==*=+.这同样适用于 andnot.

You can thing about the difference between those as different between + and *: ||==* and or=+. The same applies to and and not.

你真的应该注意这一点.

You should really pay attention to that.

我个人更喜欢 || 操作符,因为它的语义很好理解,避免使用 .

Personally I prefer || operator as its semantics is well understood and avoid or.

虽然在许多情况下感觉"or 更友好(请参阅我的代码示例),即使在微不足道的情况下,它也是错误的来源.

While it 'feels' like or is more friendly in many cases (see my code sample), even in trivial ones, it is a source of bugs.

这篇关于Ruby:比较级之间有什么区别:“||"?和“或"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆