blank?

written by mark on February 4th, 2008 @ 06:05 AM

I have confused myself in the past when trying to use this method in the wrong places.

Using the method #blank? is useful but confusion can come from not knowing when it can be applied. For example rails adds the method for you so if you write a standalone ruby program it won’t be there unless you add it yourself. Also you may confuse having a nil object with not having an object. Example…


puts @a 
puts b

This little script will choke on the second line since it is not defined. So while you can call methods on a nil class you cannot call methods on an undefined variable. Using the @ symbol will implicitly scope the variable as an object and give you a nil class if you don’t define it. So now when we do this.


puts @a.blank?
puts b.blank?

The first variable is a Nil object that supports the #blank? method but the second is undefined and will choke.

Post a comment