juniorRuby
What is the difference between true, false, and nil in conditional statements?
Updated May 17, 2026
Short answer
In Ruby, only false and nil are 'falsy'. Everything else—including the number 0, empty strings "", and empty arrays []—evaluates to 'truthy'.
Deep explanation
Ruby has a straightforward rule for truth values: any object that is not explicitly false or nil is considered true in a conditional context. This sets Ruby apart from languages like Python, JavaScript, or C, where empty collections, empty strings, or zero integers resolve to false.
Real-world example
When checking if an array or a string contains data before processing it, you cannot simply write if array. You must use methods like !array.empty? or ActiveSupport's if array.present?.
Common mistakes
- Writing `if user.age` assuming that if age is 0, it evaluates to false, causing validation logic to be bypassed.
Follow-up questions
- How do you check if an object is strictly nil?
- What is the result of double negation (`!!`) on a truthy value?