update: I mistyped 2 variables…so embarrassing. thanks everyone for the effort!
sorry i find I typed into wrong value for 2 different env, 1st one i set sort = 0 and rating=1 but then in irb I set sort=1 and rating=0. since for a and b when a=1 it only evaluates b so in the 2nd situation I get false. now it comes clear. sorry for the careless, thanks everyone for the effort!
======================
my question:
I have 2 variables, sort and rating, but both sort&&rating == 1 and sort and rating ==1 behave differently in 2 different env as below:
1st, in the debugger env of rails when I execute rails server --debug, they all return true:
(rdb:130) rating
1
(rdb:130) sort
0
(rdb:130) sort and rating == 1
true
(rdb:130) sort && rating == 1
true
(rdb:130) (sort && rating) == 1
true
2nd, in normal irb, they all return false:
irb(main):001:0> sort = 1
=> 1
irb(main):002:0> rating = 0
=> 0
irb(main):003:0> sort||rating
=> 1
irb(main):004:0> sort and rating
=> 0
irb(main):005:0> sort and rating == 1
=> false
irb(main):006:0> sort && rating == 1
=> false
irb(main):007:0> 0 == true
=> false
irb(main):008:0>
I know Ruby is so dynamic that…is this situation possible?
First, in a Boolean context, zero evaluates as true in Ruby, not false. The only things that are false are
falseitself andnil. Everything else – including empty arrays, empty strings, numerical zeroes – is considered true.Second, the Boolean operators might not behave quite the way you think. Since
andand&&are the same except for precedence, and likewiseorand||. I’ll just refer to them generically asANDandOR. These are the rules:a AND breturns a if a is false, b if a is true.a OR breturns a if a is true, b if a is false.So they are really just shorthand for
if a then b else aandif a then a else b, respectively. The truth tables are the familiar ones, but the actual result is always identical with one of the inputs, rather than some newly-constructed pure Boolean value.Despite the precedence difference, both versions of this:
equate to this:
So, since sort is zero, which is true, the above expression returns the value of
(rating == 1), which is also true (and, in fact,true). Had you gone withrating AND sort == 1, it would befalse, becausesortis not 1.You can see where the other results come from if you follow the rules given above.
Note: as mentioned by Andrew in the comments, all these various ‘true’ values are still distinct from
trueitself; 0 may be considered true, but0 == trueis still false. (This applies in any language that allows non-Booleans in a conditional context; for instance, in C, any nonzero number is true, but that doesn’t mean that any two nonzero numbers are equal.)Since this can be confusing, especially in verbal or plain text communication where you can’t use formatting tricks like true/
true, Rubyists often take a page from Stephen Colbert and refer to the true-but-not-necessarily-truevalues collectively as “truthy”, and their opposites as “falsy” (or “falsey”; I’ve seen both spellings). This convention is also used in other languages (notably by Douglas Crockford in his JavaScript book) to distinguish the actual Boolean values from other values that evaluate as true or false in conditional contexts.ETA: As for your result of
sort and rating==1returningfalsefromirb, I can’t reproduce it. In both 1.8.7 and 1.9.3, I get the expected output based on the above: