All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

The difference between blank?, present?, empty? and nil? in Ruby

Post cover: The difference between blank?, present?, empty? and nil? in Ruby
This content has been automatically translated from Ukrainian.
In Ruby and Rails, you often see the methods blank?, present?, empty?, and nil?. They seem similar, but actually have different origins and behaviors. Let's figure out when to use each method.

nil? - basic Ruby method

The nil? method is part of Ruby. It returns true if the object is exactly nil.
nil.nil?      # => true
"".nil?       # => false
[].nil?       # => false
0.nil?        # => false
It is used to check whether an object exists at all.

empty? — for collections and strings

The empty? method is defined in the String, Array, Hash, Set classes, and some others. It checks whether the collection or string is empty, meaning it contains no elements or characters.
"".empty?     # => true
[].empty?     # => true
{}.empty?     # => true
nil.empty?    # => NoMethodError
If an object does not support empty?, you will get an error. Therefore, this method should only be used when you are sure of the variable's type.

blank? - from ActiveSupport (Rails)

The blank? method comes from the ActiveSupport library that comes with Rails. It is a more "intelligent" version of empty? because it takes into account nil, spaces, and empty collections.
nil.blank?       # => true
"".blank?        # => true
"   ".blank?     # => true
[].blank?        # => true
{}.blank?        # => true
false.blank?     # => true
0.blank?         # => false
blank? is convenient to use in Rails code when you need to check the "emptiness" of any object without exceptions.

present? - from ActiveSupport (Rails)

The present? method is simply the opposite of blank?. It returns true if the object is not empty.
"Ruby".present?   # => true
"".present?       # => false
nil.present?      # => false
[1, 2].present?   # => true
It is used for conditions:
puts "Hello!" if name.present?
In short:
  • nil? - standard Ruby, checks only for nil.
  • empty? - standard Ruby, for collections and strings, etc.
  • blank? - from ActiveSupport, considers nil, spaces, and empty collections.
  • present? - from ActiveSupport, the opposite of blank?.
When to use what
  • If you are writing pure Ruby without Rails - use nil? and empty?.
  • If you have Rails or ActiveSupport - it is often more convenient to use blank? / present?, as they cover all cases and do not raise errors on nil.

This post doesn't have any additions from the author yet.

Include, Extend, Prepend in Ruby: how they work and what the difference is
29 Oct 21:20

Include, Extend, Prepend in Ruby: how they work and what the difference is

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
module_function in Ruby: when module methods are available as module methods and as functions
29 Oct 21:53

module_function in Ruby: when module methods are available as module methods and as functions

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is memoization in Ruby?
30 Oct 10:17

What is memoization in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
is_a?, kind_of?, instance_of? — how does Ruby check the type of an object?
30 Oct 19:55

is_a?, kind_of?, instance_of? — how does Ruby check the type of an object?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
&& vs and — the difference in Ruby that can break your code
30 Oct 20:23

&& vs and — the difference in Ruby that can break your code

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Variables in Ruby: @, @@ and class instance variable
30 Oct 20:54

Variables in Ruby: @, @@ and class instance variable

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Middleware in Ruby on Rails and when is it used
04 Nov 10:39

What is Middleware in Ruby on Rails and when is it used

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is the Vanilla Rails approach?
14 Nov 16:48

What is the Vanilla Rails approach?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Elasticsearch and how does it work?
22 Nov 12:35

What is Elasticsearch and how does it work?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a time-series database?
22 Nov 12:42

What is a time-series database?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
22 Nov 12:49

What is VACUUM in PostgreSQL?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a B-Tree (Balanced Tree)?
22 Nov 12:58

What is a B-Tree (Balanced Tree)?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska