Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An alternative to the Rails #try method

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 benchmarks.rb


An alternative to the Rails #try method.


This library is alpha. Very alpha. It should not be used in production. It should not be used as an example of “clean code” or “best practices”. It is under heavy development, and may change, be refactored, be completely removed, at a moment's notice. The author disavows any responsibility if your code breaks via its use. Be very certain you know what you're doing if you look at this code - it is, to repeat, alpha.


The Rails #try method attempts to avoid method chain dependencies that result in errors, by instead supplying a nil value. Take the following example:

If, say, the @user object doesn't have an associated company, many errors ensue. With pure Ruby, it can be avoided by writing the following: rescue nil

To use the Rails #try method to catch these errors, one must chain calls together like so:


With Gnoll, the same behavior can be achieved using the following technique:


Gnoll also provides error reporting, to allow for inspection of precisely where the error lies:

  # NoMethodError: Undefined method 'ceo' for nil:NilClass
  #         (irb):13:in `irb_binding'
  #         Subsequent calls:
  #         :name


Copyright © 2012 Kevin W. Gisi. See MIT-LICENSE for details.

Something went wrong with that request. Please try again.