Nil

Nill is a special Ruby object used to show the presence of nothing. It’s special because there can only be one instance of the NilClass and because it is the only Ruby value that is concidered “falsy” other than the false boolean.

"".nil? => false
"foo".nil? => false
[].nil? => false
{}.nil? => false
nil.nil? => true # The only thing that nil conciders to be true

Empty

Empty is a Ruby method concerned with length which works on strings, arrays, hashes, and sets. It returns true if the length is zero. The empty method considers whitespace as characters meaning that it might not be a good choice when validating text.

"".empty? => true
[].empty? => true
{}.empty? => true
"foo".empty? => false
" ".empty? => false # whitespace are concidered characters

Blank

Blank is a Rails method similar to empty, the only difference being that it ignores whitespace so will consider a string empty if it contains just spaces.

"".blank? => true
"    ".blank? => true
[].blank? => true
{}.blank? => true

Present

Does the opposite of blank?

This is also a rails method but does the opposite of blank?

"".present? => false
"   ".present? => false
"foo".present? => true
[].present? => false
{}.present? => false

Compare

Data Class nil? empty? blank? present?
5 Integer false NoMethodError false true
”” String false true true false
” “ String false false true false
“\t\n” String false false true false
[] Array false true true false
[“foo”] Array false false false true
{} Hash false true true false
{foo: “bar”} Hash false false false true
{} Set false true true false
nil NilClass true NoMethodError true false
true TrueClass false NoMethodError false true
false TrueClass false NoMethodError true false