Why does the [removed]true == true == true) produce a syntax error?

前端 未结 3 1880
花落未央
花落未央 2021-02-01 12:11

Ruby:

true == true == true

syntax error, unexpected tEQ

3条回答
  •  臣服心动
    2021-02-01 12:48

    Association direction, which controls the order of operators having their arguments evaluated, is not defined for the == method, same as for ===, !=, =~ and <=> methods as well (all of which have the same precedence and form a separate precedence group exclusively).

    Documentation

    Thus evaluation order in case of multiple operators from the list mentioned above being chained in a row should be set explicitly via either

    • parenthesis ():

      (true == true) == true # => true
      true == (true == true) # => true
      
    • or dot operator . (can be omitted for the last equality check in a row):

      true .== true == true # => true
      

提交回复
热议问题