简体   繁体   English

ruby中的布尔逻辑'true and false == true'

[英]boolean logic in ruby 'true and false == true'

> 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? 为什么红宝石会以这种方式表现,我将如何正确使用它以免遇到此问题?

Precedence. 优先顺序。 test = true and false means this: test = true and false意味着:

(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: 使用上面的括号,或者如果要分配在最后,请使用&&代替and

test = true && false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM