i = true 和 false 在 Ruby 中是真的吗? [英] i = true and false in Ruby is true?
本文介绍了i = true 和 false 在 Ruby 中是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是不是从根本上误解了 Ruby?我已经写了大约 2 年的 Ruby 代码,今天偶然发现了这个......
Am I fundamentally misunderstanding Ruby here? I've been writing Ruby code for about 2 years now and just today stumbled on this...
ruby-1.8.7-p249 > i = true and false
=> false
ruby-1.8.7-p249 > i
=> true
有人可以解释一下这里发生了什么吗?我确定它符合规范,但对我来说似乎违反直觉...
Could somebody explain what's going on here please? I'm sure it's to spec, but it just seems counter intuitive to me...
推荐答案
&&
和 and
运算符的优先级不同,=代码>恰好介于两者之间.
The operators &&
and and
have different precedence, and =
happens to be in between.
irb(main):006:0> i = true and false
=> false
irb(main):007:0> i
=> true
irb(main):008:0> i = true && false
=> false
irb(main):009:0> i
=> false
irb(main):010:0>
第一个读作(i = true) and false
,第二个读作i = (true && false)
.
The first is read as (i = true) and false
, the second as i = (true && false)
.
这篇关于i = true 和 false 在 Ruby 中是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文