In fact, it used to be empty?
. Here's the commit that changed it to !!empty?
: https://github.com/rails/rails/commit/126dc47665c65cd129967cbd8a5926dddd0aa514
From the comments:
Bartuz:
Why double !!
? It returns the TrueClass
/ FalseClass
anynway
fxn:
Because it is a dynamic language and subject to polymorphism, you just can't rely on empty?
returning singletons, what you need to guarantee is that you return one no matter what.
The "improved" implementation however is incomplete, because you could just as well implement !
to return a non-boolean value:
class Foo
def !
nil
end
end
Foo.new.blank? #=> nil
To handle both methods (empty?
and !
), it should be implemented as:
!!(respond_to?(:empty?) ? empty? : !self)