简体   繁体   English

“或”和|之间有什么区别在红宝石?

[英]What's the difference between “or” and | in ruby?

I thought the only difference was between | 我认为唯一的区别是| and || || where | 哪里| would be equivalent to or . 等同于or But I realised the latter is incorrect and now I'm confused. 但我意识到后者是不正确的,现在我很困惑。

AMEND : I understand this question to be different from ruby bitwise or while mine is about the lack of understanding of the difference between bitwise and boolean operators as pointed out here in comments and answers. AMEND :我理解这个问题与ruby bitwise不同, 或者我的意思是缺乏对bitwise和boolean运算符之间差异的理解,正如评论和答案中所指出的那样。 Besides, in my opinion the answers to this question have been more relevant and clearer to the problem itself. 此外,在我看来,这个问题的答案对问题本身更为相关和清晰。 Flagging as duplicate would dissuade users from the better answers. 标记为重复会阻止用户获得更好的答案。

The | | operator is a binary mathematical operator, that is it does a binary OR and works on a numerical level: operator是一个二元数学运算符,它是二进制OR并且在数值级别上工作:

1 | 2
# => 3
4 | 3
# => 7
1 | 2 | 3
# => 3

This is because it's manipulating individual values as if they were binary: 这是因为它正在操纵单个值,就好像它们是二进制的:

0b01 | 0b10
# => 3 (0b11)

The || || operator is a logical one, that is it returns the first value that's logically true. 运算符是逻辑运算符,即它返回逻辑上为真的第一个值。 In Ruby only literal nil and false values evaluates as logically false, everything else, including 0 , empty strings and arrays, is true. 在Ruby中,只有文字nilfalse值的逻辑判断为false,其他所有内容(包括0 ,空字符串和数组)都为true。

So: 所以:

1 || 2
# => 1
0 || 1
# => 0

The or operator works almost exactly the same as || or运算符几乎与||完全相同 except it's at a much lower precedence. 除了它的优先级要低得多。 That means other operators are evaluated first which can lead to some problems if you're not anticipating this: 这意味着首先评估其他运营商,如果您没有预料到这一点,可能会导致一些问题:

a = false || true
# => true
a
# => true

a = false or true
# => true
a
# => false

This is because it's actually interpreted as: 这是因为它实际上被解释为:

(a = false) or true

This is because = has a higher precedence when being evaluated. 这是因为=在评估时具有更高的优先级

|| and or are special built-in operators. or是特殊的内置运营商。 Which means they could (and indeed do ) have behavior that cannot be expressed in Ruby. 这意味着他们可以(而且确实 )有不能在Ruby中表示的行为。 In particular, || 特别是, || and or are non-strict and lazy in their right operand, whereas Ruby is actually a strict and eager language. 并且or在他们的右操作数中是非严格和懒惰的,而Ruby实际上是一种严格且热切的语言。

OTOH, | OTOH, | is just a method call like any other method call. 只是一个方法调用,就像任何其他方法调用。 There is absolutely nothing special about it. 它绝对没有什么特别之处。 Which means: 意思是:

  • it is strict 这是严格的
  • it is eager 它很渴望
  • any object can choose to respond to it however it wants 任何对象都可以根据需要选择响应它

Whereas || || and or are language built-in operators, which are or是语言内置的运算符,它们是

  • non-strict in their right operand 他们的右操作数不严格
  • lazy in their right operand 懒惰他们的右操作数
  • their behavior is hard-coded and independent of any particular object, it is always the same and cannot be changed 它们的行为是硬编码的,独立于任何特定对象,它始终是相同的,不能更改

The only difference between || ||之间的唯一区别 and or is precedence: or has very low precedence (and it has the same precedence as and ). or是优先: or具有非常低的优先级(和它有相同的优先级and )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM