What is the difference between nil, empty, and blank in Ruby?

Beginner

Answer

  • nil: Represents "nothing" or "no value". Only nil object responds true to .nil?
  • empty: A method that returns true for collections with no elements (arrays, hashes, strings)
  • blank: Rails addition that returns true for nil, empty collections, and whitespace-only strings
nil.nil?        # true
"".empty?       # true
[].empty?       # true
" ".blank?      # true (Rails only)