← Back to Ruby Course | Chapter 12: Ruby Best Practices | Lesson 4 of 6

Truthiness (only nil and false are falsy)

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)

markup
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
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.