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