`
zhaoningbo
  • 浏览: 610226 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ruby的逻辑运算符and与&&

    博客分类:
  • os
阅读更多
ruby中,and与&&是有区别的,而且不在“短路”运算上。看例子:
puts true and false
puts (true and false)
puts true && false
puts true & false
////~>
true
false
false
false

翻资料发现ruby1.8.7有这么个说法:
引用

Use &&/|| for boolean expressions, and/or for control flow. (Rule of thumb: If you have to use outer parentheses, you are using the wrong operators.)
2
1
分享到:
评论
1 楼 ipenglei 2014-10-20  
优先级的区别 :

puts true and false  #相当于 (puts true) and false  
puts (true and false)  
puts true && false  #相当于 puts (true && false)
puts true & false  #相当于 puts (true & false)


加括号吧

相关推荐

Global site tag (gtag.js) - Google Analytics