nil?, empty?とblank?の違い-Ruby on Rails-[和訳]

hey, guys:)
nilとemptyとblankって一緒じゃないの?とふと疑問に思ってstack overflowで検索。。


I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails.

私は何回もRuby on Railsnil?,blank?とempty?について明白な定義がないか探している。


.nil? can be used on any object and is true if the object is nil.


.empty? can be used on strings, arrays and hashes and returns true if:


String length == 0  
Array length == 0
Hash length == 0  

Running .empty? on something that is nil will throw a NoMethodError.


That is where .blank? comes in.


It is implemented by Rails and will operate on any object as well as work like .empty? on strings, arrays and hashes.


nil.blank? == true
false.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false
0.blank? == false

.blank? also evaluates true on strings which are non-empty but contain only whitespace:


"  ".blank? == true
"  ".empty? == false

Rails also provides .present?, which returns the negation of .blank?.


Array gotcha: blank? will return false even if all elements of an array are blank.


To determine blankness in this case, use all? with blank?, for example:


[ nil, '' ].blank? == false
[ nil, '' ].all? &:blank? == true 

これは覚えなきゃいけないのかな? 使う時にわからなくなったらまたこの記事をみるとしよう!
see you!!

A concise explanation of nil v. empty v. blank in Ruby on Rails - Stack Overflow