true, false, and nil

Ruby has three tiny but important values: true, false, and nil. Each is an instance of its own class:

$ irb
>> true.class
=> TrueClass
>> false.class
=> FalseClass
>> nil.class
=> NilClass
>> exit

true and false are Ruby’s booleans. nil represents the absence of a value.

The only values that Ruby treats as falsy are false and nil. Everything else — including 0, "", and [] — is treated as truthy. This is an important rule and a frequent source of surprises for newcomers coming from other languages.

$ irb
>> if 0
?>   puts '0 is truthy'
>> end
0 is truthy
=> nil
>> if ''
?>   puts 'empty string is truthy'
>> end
empty string is truthy
=> nil
>> exit

The word nil is a contraction of the Latin nihil (nothing), or — if you squint at the history of computing — the Lisp expression "not in list". Lisp gave us a lot of vocabulary; nil is one term that stuck.

Agentic Coding Tip: Ruby’s Truthiness Rules Catch Agents Too

An AI coding agent trained on a corpus that leans heavily on JavaScript and Python will sometimes forget that Ruby’s truthiness rules are different. In JavaScript and Python, 0 and "" are falsy. In Ruby they are truthy. Only false and nil are falsy in Ruby, full stop.

The confusion shows up in validation and guard code:

# Looks right. It's wrong for a legitimate zero value.
if product.price
  process(product)
end

An agent will sometimes also write if value != nil when it meant "definitely has a value," missing that false is a possible value. Or reach for value.blank? or value.present? because those read like Python’s "is it empty?" checks, forgetting that blank?/present? are Rails extensions and not in core Ruby.

Rule to add to your project’s CLAUDE.md:

When testing "does this variable have a value," be explicit
about what counts as absent. Use `.nil?` when only `nil`
counts, `.empty?` for collections and strings, `.zero?` for
numbers. Never rely on the truthiness of `0`, `""`, `[]`, or
`{}`. Outside a Rails app, do not assume `blank?` /
`present?` exist.