• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Ruby and Rails nil?, empty?, blank?, and present?

Posted Aug 27, 2023 — Updated Jan 10, 2024

Ruby (and Rails) have many ways to test whether something is … present. But each method behaves slightly differently, and some are Ruby-native while some are added by Rails.

Here’s the summary, in case you don’t feel like reading the full details below1:

 .nil? (Ruby).empty? (Ruby).blank? (Rails).present? (Rails)
truefalseNoMethodErrorfalsetrue
falsefalseNoMethodErrortruefalse
niltrueNoMethodErrortruefalse
“”falsetruetruefalse
” “falsefalsetruefalse
{}falsetruetruefalse
[]falsetruetruefalse
“hi”falsefalsefalsetrue
0falseNoMethodErrorfalsetrue
1falseNoMethodErrorfalsetrue

.nil? (Ruby)

This method is beautiful in its simplicity, and I’ve never been confused by it. It does one very specific thing: it compares with nil. No surprises here.

.empty? (Ruby)

This method is the only one of these four that can raise an error when used on the wrong type. Empty is only available on ruby collections, which includes strings, as they are collections of characters. If you try and use it on a non-collection, like integers, booleans, or nil, it’ll raise NoMethodError.

.blank? (Rails)

The blank? method in rails seems to exist to patch the cases where .empty? can raise NoMethodError—it can be safely called on integers, booleans and nil. Otherwise it behaves just like empty does.

.present? (Rails)

Present works mostly like you would expect, but there are two confusing cases.

Confusing case one: 0

This isn’t an issue with the present method, it’s a confusing design decision in the Ruby language itself:

0.present? # true

In nearly every other language, 0 is a falsey value, but not in Ruby:

# This will return 'yes'
if 0
  return 'yes'
else
  return 'no'
end

I can understand the design decision. Zero is a value, it’s not null, but this breaks the standard set by most other languages and I’ve been burned by it many times.

Confusing case two: whitespace-only strings

Non-empty strings, containing only whitespace, are also not considered present:

" ".present? # false
" ".empty? # false
" ".blank? # true

If you think hard about it and take the literal meanings of the method names, I guess you could consider a whitespace-only string non-empty but also not present, since a whitespace-only string contains no meaning. That feels like a stretch though, and this is not what I would expect in code. Fortunately, a whitespace-only string is not a common occurrence, so this one shouldn’t burn you very often (if ever).


Notes

  1. This is also for my own future reference, I never remember these ↩︎

Filed Under: Ruby

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy