[7] pry(#<#
=> true
[8] pry(#<#
=> true
[9] pry(#<#
=> true
[10] pry(#<#
=> true
[11] pry(#<#
Object
Object#blank?
[12] pry(#<#
From: /home/saracarl/.rvm/gems/ruby-2.2.0-preview2/gems/activesupport-4.1.2/lib/active_support/core_ext/object/blank.rb @ line 16:
Owner: Object
Visibility: public
Number of lines: 3
def blank?
respond_to?(:empty?) ? !!empty? : !self
end
Let's break it down
respond_to?(:empty?) ?
if the object has a method called empty?
!!empty?
return the results of empty?, "not"-ed twice. This means if the response to emtpy? is "true" it will return true (!(!true)). If the response is false it will return false (!(!false)) If the response is "nil" it will return false (!(!nil)). It covers the case where empty? returns nil instead of true or false -- if the object.empty? is nil, then the object.blank? is false
: !self
Else return !self -- if self is nil, return true, otherwise return false.
No comments:
Post a Comment