为什么表达式(true == true == true)会产生语法错误? [英] Why does the expression (true == true == true) produce a syntax error?

查看:153
本文介绍了为什么表达式(true == true == true)会产生语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby :

 true == true == true
 

语法错误,意外的tEQ

vs. JavaScript :

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

vs. C :

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

=====方法定义关联方向,该方向控制操作符对其参数进行求值的运算符的顺序. >,=~<=>方法(所有方法都具有相同的优先级,并且排他地形成一个单独的优先级组).

文档

因此,在上述列表中有多个运算符排成一行的情况下,评估顺序应通过任一方法明确设置

  • 括号():

    (true == true) == true # => true
    true == (true == true) # => true
    

  • 或点运算符.(对于连续的最后一次相等检查,可以省略 ):

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

Ruby:

true == true == true

syntax error, unexpected tEQ

vs. JavaScript:

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

vs. C:

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

解决方案

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
    

这篇关于为什么表达式(true == true == true)会产生语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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