memo
- Every value is true except false and nil
- Unlike in a lot of languages, the number zero is true in Ruby
- If you need to differentite between false and nil, either use the nil? method or == operator with false as the left operand.
Effective Ruby
true and flase act like global objects, and like any object.
[3] pry(main)> true.class
=> TrueClass
[4] pry(main)> false.class
=> FalseClass
Detect false
false as the left operand
if false == x
...
end
==
operator of false is used and FalseClass#==
is called. If the right operaend is also false object. It returns true.
Bad practice: put false as right operand.
[5] pry(main)> class Bad
[5] pry(main)* def == (other)
[5] pry(main)* true
[5] pry(main)* end
[5] pry(main)* end
=> :==
[6] pry(main)> false == Bad.new
=> false
[7] pry(main)> Bad.new == false
=> true
0 is not false!
[14] pry(main)> if 0
[14] pry(main)* p 'I am TRUE'
[14] pry(main)* end
"I am TRUE"
=> "I am TRUE"
Even nil responseds "Hi, I am nil"
nil.nil?
[13] pry(main)> nil.nil?
=> true
Why 0 is not false?
This could be worth to read.
https://gist.github.com/jfarmer/2647362
Many programming languages, including Ruby, have native boolean (true and false) data types. In Ruby they're called true and false. In Python, for example, they're written as True and False.
But oftentimes we want to use a non-boolean value (integers, strings, arrays, etc.) in a boolean context (if statement, &&, ||, etc.). So someone designing a language has to decide what values count as "true" and what count as "false." A non-boolean value that counts as true is called "truthy," and a non-boolean value that counts as false is called "falsey."
http://en.wikipedia.org/wiki/Logical_connective
http://en.wikipedia.org/wiki/Logical_conjunction
Remember: only true and false are booleans. nil is not a boolean. 0 is not a boolean. [1,2,3] is not a boolean. The string "apple" is not a boolean. When used in a context where a boolean is expected, Ruby evaluates them as boolean.
Programming languages are software, too! That means the people who built Ruby had to decide what is truthy and what is falsey. Different languages make different decisions.
In Ruby only false and nil are falsey. Everything else is truthy (yes, even 0 is truthy).
In some languages, like C and Python, 0 counts as false. In fact, in Python, empty arrays, strings, and hashes all count as false. In JavaScript, empty strings count as false, but empty arrays count as true.
Some languages (so-called "strictly typed" languages) go to the other extreme. true evaluates to true, false evaluates to false, and anything else will cause the compiler or interpreter to yell at you: "You can't evaluate a non-boolean value as a boolean!"
The point is only that different languages can decide what counts as true and what counts as false. There's nothing inherent in an empty string, for example, that make false. The various choices could be reasonable or unreasonable to varying degrees — it'd be strange to ever see the integer 1 or the string "apples" acting as false, for example — but ultimately it's a convention of the programming language.
ref:
Effective Ruby