赋值运算符真的“只是"吗?操作员? [英] Is the assignment operator really "just" an operator?

查看:47
本文介绍了赋值运算符真的“只是"吗?操作员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是由this 关于 SO 的讨论,这并没有得出可以真正解释问题的答案.我在重写"在这里以稍微不同的方式,因为我想更清楚真正的问题是什么,因此希望在这里得到答案.

My question was triggered by this discussion on SO, which did not lead to an answer that would really explain the issue. I am "rewriting" it here in a slightly different way, because I want to make it more clear what the real problem is and therefore hope to get an answer here.

考虑以下两个 Ruby 表达式:

Consider the following two Ruby expressions:

  1. 1 * a - 3
  2. 1 &&a = 3

从Ruby优先级表中,我们知道这里提到的运算符中,*的优先级最高,其次是-,然后是&& 最后通过 =.

From the Ruby precedence table, we know that of the operators mentioned here, * has the highest precedence, followed by -, then by && and finally by =.

表达式没有括号,但是 - 正如我们可以在 irb 中验证的那样,在第一种情况下为 a 提供合适的值 - 它们被评估为括号分别写为(1*a) - 3,分别为1 &&(a=3).

The expressions don't have parenthesis, but - as we can verify in irb, providing a suitable value for a in the first case - they are evaluated as if the bracketing were written as (1*a) - 3, respectively 1 && (a=3).

第一个很容易理解,因为 * 绑定比 - 更强.

The first one is easy to understand, since * binds stronger than -.

第二个不能这样解释.&& 绑定比 = 强,所以如果只考虑优先级,解释应该是 (1 && a) = 3.

The second one can't be explained in this way. && binds stronger than =, so if precedence only would matter, the interpretation should be (1 && a) = 3.

结合性(= 是右结合而 - 是左结合)也不能解释这种效果,因为结合性只有在我们有几个运算符时才重要相同的类型(例如 xyzx=y=z).

Associativity (= is right-associative and - is left-associative) can't explain the effect either, because associativity is only important if we have several operators of the same kind (such as x-y-z or x=y=z).

赋值运算符中一定有一些特殊规则,我在我检查的文档中没有找到,特别是 assignment语法.

There must be some special rule in the assignment operator, which I did not find in the docs I checked in particular the docs for assignment and syntax.

有人能指出,赋值运算符的这种特殊行为记录在哪里吗?还是我在这里错过/误解了什么?

Could someone point out, where this special behaviour of the assignment operator is documented? Or did I miss / misunderstand something here?

推荐答案

来自文档:https://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#assign

赋值表达式用于将对象赋值给变量等.赋值有时用作局部变量或类常量的声明.赋值表达式的左侧可以是:

Assignment expression are used to assign objects to the variables or such. Assignments sometimes work as declarations for local variables or class constants. The left hand side of the assignment expressions can be either:

变量变量 `=' 表达式

variables variables `=' expression

右边有一个表达式,所以将表达式的结果赋值给变量.

On the right there is an expression, so the result of the expression is assigned to the variable.

因此,您应该在遵循优先级之前查找表达式 (*).

1 &&a = 3 基本上是两个链式"表达式:

1 && a = 3 are basically two "chained" expressions:

31 &&3

也许它更具可读性,因为:<代码>1 &&a = 3 + 4 其中表达式为 3 + 41 &&7,见:

Maybe it is more readable as: 1 && a = 3 + 4 where the expressions are 3 + 4 and 1 && 7, see:

1 && a = 3 + 4 #=> 7
1 && 7 #=> 7
res = 1 && a = 3 + 4
res #=> 7

<小时>

(*) 优先级表也有助于查找表达式(在 运算符表达式 段落的链接文档中查找优先级表):


(*) The precedence table also helps to find the expression (Find the precedence table in the linked doc at the Operator expressions paragraph):

表格表单"中 = 上方的内容由 = 赋值的表达式,下面没有.

What's above the = in the table "forms" an expression to be assigned by =, what's below does not.

例如:

1 + 3 and 2 + 4 #=> 4
a = 1 + 3 and b = 2 + 4 #=> 4
(a = 1 + 3) and (b = 2 + 4) #=> 4
a = (1 + 3 and b = 2 + 4) #=> 6

<小时>

您还可以根据优先级表检查这些示例:


You can also check these examples respect to the precedence table:

1 && 3 #=> 3
1 && a = 3 #=> 3
a #=> 3

3 and 1 #=> 3
3 and b = 1 #=> 3
b #=> 1

2 ** c = 2 + 1 #=> 8
c #=> 3

d = 2 ** 3
d #=> 8

e = 3
e **= 2
e #=> 9

这篇关于赋值运算符真的“只是"吗?操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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