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? but nil? in Ruby

Post cover: The difference between blank?, present?, empty? but nil? in Ruby
This content has been automatically translated from Ukrainian.
Methods are often seen in Ruby and Rails blank?, present?, empty? i nil?. They seem similar, but actually have different sources and behaviors. Let's figure out when which method should be used.

nil? - basic Ruby method

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
Used to check if there is an object at all.

empty? — for collections and lines

Method empty? defined in the classes of String, Array, Hash, Set, and some others. He checks whether collection or string is empty, that is, they do not contain elements or symbols.
".empty?     # => true
[].empty?     # => true
{}.empty?     # => true
nil.empty?    # => NoMethodError
If the object does not support empty?, get an error. Therefore, this method should only be used when you know exactly the type of variable.

blank? - with ActiveSupport (Rails)

Method blank? appears thanks to the library ActiveSupport, which goes with Rails. It is a more "smart" 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? convenient to use in Rails code when you want to check the “void” of any object without exception.

present? - with ActiveSupport (Rails)

The present method? is simply the opposite blank?. It returns true if the object not empty.
"Ruby".present?   # => true
"".present?       # => false
nil.present?      # => false
[1, 2].present?   # => true
Used for conditions:
puts "Hello!" if name.present?
Briefly:
  • nil? - standard Ruby, only checks nil.
  • empty? - standard Ruby, for collections and lines, etc.
  • blank? - with ActiveSupport, takes into account nil, spaces, and empty collections.
  • present? - with ActiveSupport, opposite to blank?.
When to use something
  • If you write pure Ruby without Rails - use nil? and empty?.
  • If you have Rails or ActiveSupport - more often convenient blank? /present?, because they cover all cases and do not cause errors on nil.

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

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

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

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

module_function in Ruby: When module methods are available as modular 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 object type?
30 Oct 19:55

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

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

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

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

The variables in Ruby are @, @@ and class instance variable

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

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

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

What is 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 B-Tree (Balanced Tree)?
22 Nov 12:58

What is B-Tree (Balanced Tree)?

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