Truthy and Falsy (Ruby)
In Ruby only nil and false are falsy; every other value — including 0, "", and [] — is truthy. This simple rule governs how all conditionals behave.
Ruby
Ruby's truthiness is unusually clean: exactly two values count as false in a boolean context, nil and false. Everything else is true, which surprises people from languages where 0 or empty collections are falsy. So if 0 runs the block, and if "" does too.
This means to test for an empty string or array you must be explicit — str.empty?, arr.any? — rather than relying on truthiness. The pattern value || default works precisely because only nil/false trigger the fallback.
The && and || operators return actual operands (not just booleans), enabling idioms like name = input || "guest" and config = options[:x] && options[:x].upcase. Understanding truthiness is essential for reading idiomatic Ruby conditionals.
Examples
Ruby
empty = ""
puts "zero is truthy" if 0
puts "empty string is truthy" if empty
puts "nil is falsy" unless nilOutput:
zero is truthy
empty string is truthy
nil is falsy