[PYTHON] There are two interpretations of VBScript a = b = c

What is the equivalent Ruby code to the following code written in VBScript?

a = b = c

answer

Depending on the context in which the code is written, it can be one of the following two:

a = (b == c)
(a == b) == c

VBScript handles both assignment and comparison with one operator =. Also, in VBScript, assignments are statements, not expressions. As a result, people who have learned languages with separate assignment and comparison operators may encounter unexpected behavior when using VBScript.

In many languages, to a greater or lesser extent, the boundaries between expressions and statements are ambiguous, allowing you to write statements in contexts where expressions are likely to be required. Strictly speaking, is that a function that is provided as a sentence in one language is provided as an expression in another language. Ruby is a good sample as many of the statements provided in other languages are expressions.

#if is an expression, not a statement
a = (if hoge == fuga then 1 else 0)

#Even method definitions become expressions
a = (def hoge; end)

The Ruby specification that even if and method definitions become expressions seems to be foreign to Java programmers. Some may argue that it is overkill to formulate these.

However, we accept what was made from a sentence long ago and treat it as a formula without any discomfort. That is the assignment.

The code below can be executed as evidence that the assignment is treated as an expression. If the assignment is a statement, the result cannot be assigned to a.

a = b = c

But in VBScript, assignments are statements, not expressions. Both = of ʻa = b = c` cannot be assigned.

Therefore, the Ruby code equivalent to VBScript's ʻa = b = cis ʻa = (b == c)or (a == b) == c. There are two possibilities. When is it interpreted as the former and when is it interpreted as the latter?

Again, I remember the specification that assignments are statements rather than expressions. The former is an expression, while the latter is a sentence. In other words, the context in which this code is written depends on whether the statement is required or the expression is required.

'The context in which the expression is required
Call Func(a = b = c)
While a = b = c

'The context in which the statement is requested
a = b = c

Recommended Posts

There are two interpretations of VBScript a = b = c
ABC129 A, B, C commentary
Python a + = b and a = a + b are different
ABC127 A, B, C Explanation (python)
ABC128 A, B, C commentary (python)
ABC126 A, B, C Explanation (python)
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
Solve ABC175 A, B, C in Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
Derivation of certainty of effect in A / B testing