rubysyntax-erroroperatorsoperator-precedenceassociativity

Why does expression (true == true == true) produce a syntax error?


Ruby:

true == true == true

syntax error, unexpected tEQ

vs. JavaScript:

true == true == true
// => true

vs. C:

1 == 1 == 1
// => 1

Solution

  • 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