In Ruby, only nil and false are treated as false. Everything else is true.
Yes, that includes 0. The following statement will evaluate as true:
So to check if something is nil you can use one of these methods:
Or, if you need to initialize an instance variable that you never want to be nil, here’s the sexiness to use:
This will check if the variable is nil, and if so, assign it to “Waaat?”. Otherwise, it will leave it untouched.
Objective-C uses YES and NO or non-zero and 0 instead of true and false. An object pointer that does not point to anything is nil.
nil is the exact same as 0. nil is used for objects, while 0 for primitive types (int, double, etc).
Both nil and 0 evaluate to false in Objective C, so both of these print statements would not execute.
1 2 3 4
I often get confused about whether nil is true or false, so I hope this helps somebody.