public
Description: ignore_nil { so.i.dont.raise.nomethoderror.on.long.chained.methods }
Homepage: http://gemcutter.org/gems/ignore_nil
Clone URL: git://github.com/ssoroka/ignore_nil.git
name age message
file .gitignore Tue Sep 22 23:48:09 -0700 2009 remove manifest [ssoroka]
file README.rdoc Tue Sep 22 23:42:16 -0700 2009 update README with some more details. bump to 1... [ssoroka]
file Rakefile Tue Sep 22 23:42:16 -0700 2009 update README with some more details. bump to 1... [ssoroka]
file ignore_nil.gemspec Tue Sep 22 23:48:09 -0700 2009 remove manifest [ssoroka]
file init.rb Tue Sep 22 23:42:16 -0700 2009 update README with some more details. bump to 1... [ssoroka]
directory lib/ Fri Apr 10 14:59:15 -0700 2009 Get rid of extra aliases, I don't think anyone ... [ssoroka]
directory test/ Thu Dec 11 09:17:01 -0800 2008 added support for nil.id raising RuntimeError w... [ssoroka]
README.rdoc

ignore_nil {}

DESCRIPTION

ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much safer than "rescue nil", since it only catches NoMethodError on nil objects.

ignore_nil {} will either return the last thing evaluated in the block, or nil if a NoMethodError is raised calling a method on a nil object. Any other exceptions raised in the block are not handled, and left for the application to resolve. More details below.

INSTALLATION

as a gem:

    sudo gem install ssoroka-ignore_nil

as a plugin:

    script/plugin install git://github.com/ssoroka/ignore_nil.git

USAGE

    ignore_nil { user.profile.photo }

which is much cleaner than, say,

    user && user.profile && user.profile.photo

and much much safer than

    user.profile.photo rescue nil

which will eat any error, even if it’s one you really want to see.

TELL ME MORE!

The plugin is really rather simple; here’s the ignore_nil method:

    def ignore_nil(&block)
      begin
        yield
      rescue NoMethodError, RuntimeError => e
        if (e.message =~ /You have a nil object when you didn't expect it/) ||
            (e.message =~ /undefined method `.*?' for nil:NilClass/) || (e.message =~ /^Called id for nil/)
          return nil
        else
          raise e
        end
      end
    end

What’s interesting about this is it catches both NoMethodError and RuntimeError, both of which can occur if a method unexpectedly returned nil and you called a method on it, but ONLY if the error message matches! This means legitimate NoMethodError and RuntimeError messages will not be bothered by ignore_nil, and will still raise in your application as you expect.

I’ve used this in a production application since about mid/late 2008, I’d consider it very stable. Feedback welcome!

AUTHOR

Steven Soroka [@ssoroka](www.twitter.com/ssoroka) [blog.stevensoroka.ca](http://blog.stevensoroka.ca)