Rails Object # blank? vs. String # empty? confusion

Do Rails docs have this information for Object#blank?

An object is empty if its false, empty or a string of spaces. For example, "," ", nil, [] and {} are empty.

But the source of this method is as follows:

 # File activesupport/lib/active_support/core_ext/object/blank.rb, line 12 def blank? respond_to?(:empty?) ? empty? : !self end 

Now when I open my handy little command line and type ruby -e 'p " ".empty?' , it returns false. This means that Rails has to say that this is an empty value when this is clearly not the case. But! I open my rails console and I type " ".empty? and get false as my earlier direct command line. But, I type ".blank?" and I believe how Rails promises me.

What am I missing in understanding how the Rails method is blank? works with the empty? method empty? for String?

+7
source share
1 answer

Are the rails quite complicated in how he documents his blank? method blank? . Despite the fact that Object#blank? claims it also detects strings with spaces, is it implemented using String#blank? for handling whitespace and Object#blank? to catch the general case. ( blank? also defined for several other classes to save time.)

activesupport / lib / active_support / core_ext / object / blank.rb, line 66 :

 class String def blank? self !~ /\S/ end end 
+10
source

All Articles