boolean logic in ruby 'true and false == true'

♀尐吖头ヾ 提交于 2019-12-24 13:25:49

问题


> test = false and true
=> false
> test
=> false
> test = true and false #this is the point I don't understand!
=> false
> test
=> true

Why does ruby behave in this way and how would I use it correctly to not run into this problem?


回答1:


Precedence. test = true and false means this:

(test = true) and false  

not this:

test = (true and false)

Use parentheses as above, or && instead of and, if you want the assignment to come last:

test = true && false


来源:https://stackoverflow.com/questions/10249164/boolean-logic-in-ruby-true-and-false-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!