nil (Ruby)
nil is Ruby's representation of 'nothing' or 'no value' — the sole instance of NilClass. It is one of only two falsy values in Ruby (the other is false).
Ruby
nil stands in for the absence of a value: an uninitialized instance variable, a missing hash key, or a method with nothing to return all give nil. Being an object, it responds to methods like nil?, to_s (gives ""), to_a (gives []), and inspect.
Crucially, only nil and false are falsy in Ruby; everything else — including 0, empty strings, and empty arrays — is truthy. So a conditional like if value tests 'not nil and not false', which is a common source of confusion for those coming from other languages.
Handle nil with the safe navigation operator &. (so user&.name doesn't crash when user is nil), the &.fetch / || default pattern, or explicit value.nil? checks. nil-handling is a routine part of writing robust Ruby.
Examples
Ruby
x = nil
puts x.nil?
puts x.to_s.inspect
puts (x || "default")Output:
true
""
default