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:
Dir[File.join(RAILS_ROOT, 'lib', 'ext', '*.rb')].each do |f|
base = File.basename(f, '.rb')
require "ext/#{base}"
end
One project, for example, has files:
active_record_compare.rb
array.rb
enumerable.rb
file.rb
integer.rb
nil_class.rb
range.rb
string.rb
web_agent.rb
And my nil_class.rb contains:
class NilClass
# Allowing a chain like: value.nonblank? || 'default value'
def nonblank?
self
end
# so it plays nicely with Numeric#nonzero?
def nonzero?
self
end
end