Skip to content
Justice Tech School

nil and Truthiness

Only false and nil are falsy. 0 and empty strings are truthy.

Example
puts !!0, !!"", !!nil, !!false
x = nil
puts x.nil?, (x || "default"), x&.length.inspect

Result

true
true
false
false
true
default
nil

Exercise

Safe navigation.

x.length

Quiz Test

Is 0 truthy in Ruby?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.