Nil? and blank?

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

An object is blank if it’s nil, empty, or a whitespace string. If it’s
nil, it’s nil.

blank is:

“”
" "
nil
[]
{}
false

any of the above applied with .blank? will return true.

“”.blank? # returns true
[].blank? # returns true
{""=>""}.blank? # returns false
{}.blank? # returns true

On Oct 9, 2007, at 4:03 PM, Shandy N. wrote:

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

class Numeric #:nodoc:
def blank?
false
end
end

-Rob

Rob B. http://agileconsultingllc.com
[email protected]
+1 513-295-4739
Skype: rob.biedenharn

Thanks all, that helps out a lot.

Use the Source…
(vendor/rails/activesupport/lib/active_support/core_ext/blank.rb)

I’d like to have a .blank? for Time and date where 0000-00-00 00:00:00
etc is considered blank.

Where’s the right place w/in a Rails project to add an extension to
Ruby’s classes to accomplish that?

– gw

On Nov 3, 2007, at 5:35 AM, Rob B. wrote:

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:

Makes sense. Thanks.

– gw

On Nov 3, 2007, at 1:22 AM, Greg W. wrote:

Use the Source…
(vendor/rails/activesupport/lib/active_support/core_ext/blank.rb)

I’d like to have a .blank? for Time and date where 0000-00-00 00:00:00
etc is considered blank.

Where’s the right place w/in a Rails project to add an extension to
Ruby’s classes to accomplish that?

– gw

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

That should give you some ideas.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]