Truthiness in Ruby
nil and false.
Truthiness in Ruby is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Truthiness?
In a condition, Ruby does not require a strict boolean. Any value is treated as either truthy or falsy. Knowing the rule prevents subtle bugs.
if "hello"
puts "Strings are truthy"
endOnly Two Falsy Values
Ruby's rule is delightfully simple: only false and nil are falsy. Everything else is truthy.
puts "nil is falsy" unless nil
puts "false is falsy" unless falseZero Is Truthy
Unlike many languages, 0 is truthy in Ruby. So is an empty string and an empty array. Watch out if you came from C or Python.
if 0
puts "0 is truthy in Ruby"
end
if ""
puts "empty string is truthy too"
endMeet nil
nil represents the absence of a value. Methods often return nil when there is nothing to give back, such as a missing hash key.
user = { name: "Ada" }
puts user[:email].inspect # missing key returns nilChecking for nil
Use nil? to test explicitly. It returns true only for nil.
x = nil
y = 0
puts x.nil?
puts y.nil?The && Operator
&& returns the first falsy operand, or the last operand if all are truthy. It short-circuits: the right side is not evaluated if the left is falsy.
puts (nil && "never").inspect
puts ("a" && "b").inspectThe || Operator
|| returns the first truthy operand. This makes it perfect for default values.
name = nil
display = name || "Guest"
puts displayConditional Assignment ||=
a ||= b assigns b to a only if a is currently nil or false. It is the idiomatic way to set a default.
config = {}
config[:timeout] ||= 30
config[:timeout] ||= 99 # already set, ignored
puts config[:timeout]Beware the Falsy Default
Because false is falsy, || can overwrite a legitimate false value. When false is meaningful, check nil? instead.
enabled = false
result = enabled || true # oops, becomes true
puts result
safe = enabled.nil? ? true : enabled
puts safeThe Safe Navigation Operator
&. calls a method only if the receiver is not nil, otherwise it returns nil instead of raising an error.
name = nil
puts name&.upcase.inspect # no NoMethodError
name = "ruby"
puts name&.upcasePutting It Together
Truthiness rules let you write expressive, compact code, but keep the edge cases in mind:
- Only
nilandfalseare falsy. 0,"", and[]are all truthy.- Use
nil?when a realfalsematters.
values = [0, "", [], nil, false]
values.each do |v|
puts "#{v.inspect} => #{v ? "truthy" : "falsy"}"
endQuick Check
Test your grasp of Ruby truthiness.
Recap
You learned truthiness in Ruby:
- Only
nilandfalseare falsy. nil?checks for nil explicitly.&&and||return operands and short-circuit.||=sets defaults;&.guards against nil receivers.
username = nil
username ||= "anonymous"
puts "Welcome, #{username}"Frequently asked questions
Is the “Truthiness in Ruby” lesson free?
Yes — the full text of “Truthiness in Ruby” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Truthiness in Ruby”?
nil and false. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Truthiness in Ruby” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if, unless, ternary
- case/when Expressions
- Truthiness in Ruby
- Guard Clauses