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.
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:
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:
This method is about size, not missingness. A value can be empty without being nil.
For example:
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:
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:
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:
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:
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 isnil.' - '
empty?checks zero length on types that implement it, such as strings and arrays.' - '
blank?is a Rails helper that treatsnil,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.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.