ruby中的布尔逻辑'true and false == true' [英] boolean logic in ruby 'true and false == true'

查看:303
本文介绍了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?

推荐答案

优先级. test = true and false意味着:

(test = true) and false  

不是这个:

test = (true and false)

使用上面的括号,或者,如果要分配到最后,请使用&&代替and:

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

test = true && false

这篇关于ruby中的布尔逻辑'true and false == true'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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