Truthiness (only nil and false are falsy)
In this page:
Truthiness (only nil and false are falsy)
In Ruby, every value is truthy in a boolean context except nil and false -- notably, unlike many other languages, 0 and "" (empty string) are both truthy. This is a frequent surprise for developers coming from languages where 0 or empty collections are falsy. Knowing this rule precisely avoids subtle conditional bugs.
Warning: Unlike many other languages, 0 and "" (empty string) are truthy in Ruby -- only nil and false are falsy.
Example: Truthiness (only nil and false are falsy)
puts "0 is truthy" if 0
puts "empty string is truthy" if ""
puts "this won't print" if nil
puts "this won't print either" if false
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: