How does truthiness work in Ruby, and what is nil?
Assesses fundamental understanding of Ruby conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
In Ruby only nil and false are falsy. Everything else, including 0, "", empty arrays and empty hashes, is truthy. This differs from many languages and is a common source of bugs.
puts "truthy" if 0 # prints, 0 is truthy
puts "yes" if [] # prints, [] is truthy
value = nil
value ||= 10 # assign if nil or false
puts value.inspect # 10
nil is the single instance of NilClass and responds to nil?; Object#nil? returns false for everything else. Test emptiness explicitly with empty? or, in Rails, present?. ||= assigns when the variable is nil or false.
Calling a method on nil raises NoMethodError, but safe navigation short-circuits: user&.profile&.name. nil.to_s is an empty string and nil.to_a is an empty array, which is why Array(nil) and String(nil) are useful conversions.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.