And/&& 的运算符优先级在红宝石中

发布于 2024-08-12 22:55:00 字数 421 浏览 3 评论 0原文

我有一个关于 Ruby 中的 and/&&/= 关键字的问题。

ruby 文档说上述关键字的优先级是:(1)&&、(2)=、(3)and。

我写了这段代码:

def f(n) 
 n
end

if a = f(2) and  b = f(4) then  
    puts "1) #{a} #{b}" 
 end

if a = f(2) &&  b = f(4) then   
    puts "2) #{a} #{b}"     
end

输出是:

1) 2 4 [预期]

2) 4 4 [为什么?]

由于某种原因使用 &&导致 a 和 b 的计算结果都为 4?

I have a question regarding the and/&&/= keywords in Ruby.

The ruby docs say that the precedence for the mentioned keywords is: (1)&&, (2)=, (3)and.

I have this snippet of code I wrote:

def f(n) 
 n
end

if a = f(2) and  b = f(4) then  
    puts "1) #{a} #{b}" 
 end

if a = f(2) &&  b = f(4) then   
    puts "2) #{a} #{b}"     
end

The output is:

1) 2 4 [Expected]

2) 4 4 [Why?]

For some reason using the && causes both a and b to evaluate to 4?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里寻她 2024-08-19 22:55:01

我不太明白你问的问题。我的意思是,在提出问题之前,您已经自己给出了答案:&&=< 绑定更紧密/code> 而 and 的结合不如 = 紧密。

因此,在第一种情况下,表达式的计算如下:

( a=f(2) )  and  ( b=f(4) )
( a=  2  )  and  ( b=f(4) )
      2     and  ( b=f(4) ) # a=2
      2     and  ( b=  4  ) # a=2
      2     and        4    # a=2; b=4
                       4    # a=2; b=4

在第二种情况下,计算如下:

a   =   (  f(2) && ( b=f(4) )  )
a   =   (    2  && ( b=f(4) )  )
a   =   (    2  && ( b=  4  )  )
a   =   (    2  &&       4     ) # b=4
a   =                    4       # b=4
                         4       # b=4; a=4

I don't quite understand the question you are asking. I mean, you have already given the answer yourself, before even asking the question: && binds tighter than = while and binds less tightly than =.

So, in the first case, the expression is evaluated as follows:

( a=f(2) )  and  ( b=f(4) )
( a=  2  )  and  ( b=f(4) )
      2     and  ( b=f(4) ) # a=2
      2     and  ( b=  4  ) # a=2
      2     and        4    # a=2; b=4
                       4    # a=2; b=4

In the second case, the evaluation is as follows:

a   =   (  f(2) && ( b=f(4) )  )
a   =   (    2  && ( b=f(4) )  )
a   =   (    2  && ( b=  4  )  )
a   =   (    2  &&       4     ) # b=4
a   =                    4       # b=4
                         4       # b=4; a=4
放肆 2024-08-19 22:55:01

原因很简单:优先。正如你所说,顺序是:

  1. &&
  2. =

由于 && 优先于 =,因此该语句的计算方式如下:

if a = (f(2) && (b = f(4))) then 

其结果是:

if a = (2 && 4) then

When x and y 是整数,x && y 返回y。因此 2 && 4 结果为a = 4

为了进行比较,第一个的评估如下:

if (a = f(2)) and  (b = f(4)) then 

The reason is simple: precedence. As you say, the order is:

  1. &&
  2. =
  3. and

Since && has precedence over =, the statement is evaluated like this:

if a = (f(2) && (b = f(4))) then 

Which results in:

if a = (2 && 4) then

When x and y are integers, x && y returns y. Thus 2 && 4 results in a = 4.

For comparison's sake, the first one is evaluated like this:

if (a = f(2)) and  (b = f(4)) then 
权谋诡计 2024-08-19 22:55:01

Ruby 编程 1.9 开始:

两种形式的唯一区别是优先级(and 绑定低于 &&)。

From Programming Ruby 1.9:

The only difference in the two forms is precedence (and binds lower than &&).

奢望 2024-08-19 22:55:01

我不知道在这种情况下可以提供帮助的具体规则,但让我们使用操作的优先级。
使用优先级规则,我们可以将第二个表达式的计算分为几个步骤

1 f(2) &&  b => expr1
2 expr1 = f(4) => expr2
3 a = expr2

。 显然,在步骤 2 中我们得到了一个不正确的情况 - = 的左侧是右值 - 临时对象,它不能被分配任何值。我假设语法分析器在遇到这种情况时违反了表达式优先级评估的规则。
有关表达式计算的更多详细信息,请参阅此处

I do not know the specific rules that can help in this situation, but let's use the priorities of operations.
Using the rules of priorities, we can divide the computation of the second expression on several steps

1 f(2) &&  b => expr1
2 expr1 = f(4) => expr2
3 a = expr2

Obvious that in Step 2 we get an incorrect situation - on the left side of = is rvalue - temporary object, which can not be assigning by any value. I assume that syntactic analyzer break the rules of priority evaluation of expressions when encounter such situations.
More details on the calculations of expressions can be found here

旧竹 2024-08-19 22:55:01

如果您像这样修改代码,您将得到您期望的结果

def f(n) 
  n
end

if (a = f(2) and  b = f(4)) then  
  puts "1) #{a} #{b}" 
end

if (a = f(2)  and  b = f(4)) then   
  puts "2) #{a} #{b}"         
end

1) 2 4

2) 2 4

if you modify your code like this you will get what you expect

def f(n) 
  n
end

if (a = f(2) and  b = f(4)) then  
  puts "1) #{a} #{b}" 
end

if (a = f(2)  and  b = f(4)) then   
  puts "2) #{a} #{b}"         
end

1) 2 4

2) 2 4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文