Ruby
Coding
Programming
Software Development
Ruby on Rails

How to understand nil vs. empty vs. blank in Ruby

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Ruby developers regularly encounter nil?, empty?, and blank?, but they answer different questions. The easiest way to keep them straight is to remember that nil? checks for missing value, empty? checks for zero length on specific container types, and blank? is a Rails convenience that treats several "effectively empty" values as absent.

nil? Means No Value

nil is Ruby's object for "no value." The method nil? returns true only for nil itself:

ruby
1value = nil
2puts value.nil?
3puts "".nil?
4puts [].nil?

This is the narrowest test of the three. It does not ask whether a string has content or whether an array has elements. It asks only whether the object is nil.

That makes nil? appropriate when the difference between "not present" and "present but empty" matters.

empty? Means Length Zero

empty? is defined on specific classes such as String, Array, and Hash:

ruby
1puts "".empty?
2puts "hi".empty?
3puts [].empty?
4puts({}.empty?)

This method is about size, not missingness. A value can be empty without being nil.

For example:

ruby
name = ""
puts name.nil?    # false
puts name.empty?  # true

Important detail: nil.empty? raises NoMethodError because NilClass does not implement empty?.

blank? Is A Rails Convenience

blank? is not part of plain Ruby. It comes from Active Support, which is why you see it in Rails code.

It returns true for values Rails considers effectively empty, including:

  • 'nil'
  • 'false'
  • empty strings
  • whitespace-only strings
  • empty arrays and hashes

Example:

ruby
1require "active_support/core_ext/object/blank"
2
3puts nil.blank?
4puts false.blank?
5puts "".blank?
6puts "   ".blank?
7puts [].blank?
8puts({}.blank?)
9puts "hello".blank?

This makes blank? very convenient for form validation and controller code where whitespace-only user input should count as missing.

Compare The Meanings Directly

A simple table in code form makes the difference clear:

ruby
1require "active_support/core_ext/object/blank"
2
3values = [nil, "", " ", [], {}, false, "ruby"]
4
5values.each do |value|
6  puts({
7    value: value.inspect,
8    nil: value.nil?,
9    empty: value.respond_to?(:empty?) ? value.empty? : "n/a",
10    blank: value.blank?
11  })
12end

Conceptually:

  • 'nil? is strict and narrow'
  • 'empty? is structural'
  • 'blank? is semantic and Rails-oriented'

That is why they are not interchangeable.

Use The Right One For The Job

Use nil? when you care whether a variable exists at all.

Use empty? when you know the object is a string, array, or hash and you only care whether it contains elements.

Use blank? in Rails when you want a broader "has no meaningful content" check, especially for user input.

Rails also provides the inverse present?, which is often even more readable:

ruby
1require "active_support/core_ext/object/blank"
2
3name = "Alice"
4puts name.present?

That can make controller and view code easier to scan.

Truthiness Is A Separate Concept

Another source of confusion is Ruby truthiness. In Ruby, only nil and false are falsy. Empty strings and empty arrays are still truthy:

ruby
puts "empty string is truthy" if ""
puts "empty array is truthy" if []

That is why if value is not a substitute for empty? or blank?.

Common Pitfalls

One common mistake is calling empty? on a value that may be nil, which raises an exception.

Another issue is using blank? in plain Ruby scripts without Active Support loaded. The method will not exist unless Rails or the Active Support extension is present.

A third problem is forgetting that whitespace-only strings are not empty, but they are blank in Rails.

Finally, some developers treat truthiness as if it matched emptiness. In Ruby, it does not.

Summary

  • 'nil? checks only whether the object is nil.'
  • 'empty? checks zero length on types that implement it, such as strings and arrays.'
  • 'blank? is a Rails helper that treats nil, false, empty collections, and whitespace strings as blank.'
  • Empty strings and empty arrays are still truthy in Ruby.
  • Choose the method based on whether you are checking missingness, size, or meaningful content.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions