I’ve heard this question asked before but I couldn’t find the
appropriate responces that I was looking for, but what is the difference
between Object.nil? and Object.blank? Is it how one handles white and
newline characters? To me they almost seem the same but I imagine there
has to be some differences. Thanks,
I’ve heard this question asked before but I couldn’t find the
appropriate responces that I was looking for, but what is the
difference
between Object.nil? and Object.blank? Is it how one handles white and
newline characters? To me they almost seem the same but I imagine
there
has to be some differences. Thanks,
-S
Use the Source…
(vendor/rails/activesupport/lib/active_support/core_ext/blank.rb)
class Object #:nodoc:
“”, " ", nil, [], and {} are blank
def blank?
if respond_to?(:empty?) && respond_to?(:strip)
empty? or strip.empty?
elsif respond_to?(:empty?)
empty?
else
!self
end
end
end
class NilClass #:nodoc:
def blank?
true
end
end
class FalseClass #:nodoc:
def blank?
true
end
end
class TrueClass #:nodoc:
def blank?
false
end
end
class Array #:nodoc:
alias_method :blank?, :empty?
end
class Hash #:nodoc:
alias_method :blank?, :empty?
end
class String #:nodoc:
def blank?
empty? || strip.empty?
end
end
Well, for my projects, I’ve started putting these kinds of things into
a file in:
RAILS_ROOT/lib/ext/some_class_or_behavior_name.rb
and I require them in my config/environment.rb like this: