Difference between nil? empty? blank? present?

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6. I wrote it because I wanted to clarify the distinction between nil? Empty? Blank ?.

nil? ――It is in the state of "nothing exists". There is nothing in the vessel or contents. ――So you can only express it with the word nil.

name = nil 
name.nil? #=> true

――For example, note that not all of the following examples are nil.

array = [] #=> false
zero = 0 #=> false
name = ""  #=>false
hash = {} #=> false

empty? ――It is in a state of "there is a vessel but no contents". This is easy to imagine. Is it an empty plate? --However, if you use the empty? Method for __nil, an error will occur __, so be sure to check it carefully before using it.

array = [] 
array.empty? #=> true

animal = ""
animal.empty? #=> true

name = nil
name.empty? #=> false

blank? --It's a method that combines nil? And empty ?. --Both nil and empty return true, right?

array =[]
array.blank? #=> true

name = nil
name.nil? #=> true

present? ――It is in the state of "there is a vessel and there is contents". In other words, it's OK if there is content.

age = 24
age.present? #=> true

Recommended Posts

Difference between nil? empty? blank? present?
Rails existence confirmation method properly used memo (any? / Empty? / Present? (! Blank?) / Nil?)
Difference between vh and%
Difference between i ++ and ++ i
[Swift] UITextField taught me the difference between nil and ""
Null, empty string, blank
Difference between element 0, null and empty string (check in list)
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
[Ruby] Difference between match / scan
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to