Can someone explain in details this Object method (I mean syntax used here :empty? , !!empty? and !self) :
def blank?
respond_to?(:empty?) ? !!empty? : !self
end
Or maybe advise good book to read on topic.
if
selfhas got methodempty?then returndouble not empty?otherwise returnsingle not self.
not (!) is an idiomatic way in Ruby to convert any object into Boolean
single not (!) returns false for any truthy and true for any falsey object.
double not (!!) returns true for any truthy and false for any falsey object.
Falsey objects in Ruby are nil and false, any other objects are truthy.
So. Essentially respond_to?(:empty?) ? !!empty? : !self checks if current object has defined method empty? and returns true if this method returns anything truthy or false in case of falsey response. If empty? is not defined it will always return false if self is not false or nil (any falsey object).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With