Is the assignment operator really “just” an operator?

前端 未结 3 1455
遇见更好的自我
遇见更好的自我 2021-02-14 23:22

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 differe

3条回答
  •  庸人自扰
    2021-02-15 00:08

    Probably because the other interpretation does not work:

    irb(main):003:0> (1 && a) = 3
    Traceback (most recent call last):
            3: from /home/w/.rbenv/versions/2.7/bin/irb:23:in `
    ' 2: from /home/w/.rbenv/versions/2.7/bin/irb:23:in `load' 1: from /home/w/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `' SyntaxError ((irb):3: syntax error, unexpected '=', expecting `end') (1 && a) = 3 ^

    So, perhaps Ruby parenthesizes 1 && a = 3 in the only way that is legally interpretable by the language.

提交回复
热议问题