arrow_backward Back to blog

Calling Methods on Potential Nil Objects in Rails

Rails adds a pretty cool Object#try method. From Rails doc:

try(method, *args, &block)

This Invokes the method identified by the symbol method, passing it any arguments and/or the block specified, just like the regular Ruby Object#send does.

Unlike that method however, a NoMethodError exception will not be raised and nil will be returned instead, if the receiving object is a nil object or NilClass.

This is how you use it:

You can see it is not so DRY if you want to chain a lot of methods together, as is commonly the case in a Rails app. Inspired by RSpec’s stub_chain method, I wrote a little try_chain method:

This is much cleaner and you don’t see the NoMethodError exception for nil:NilClass anymore. A future version will work with arguments as in “Don’tRepeatYourself”.try(:sub, ‘Repeat’, ‘Shoot’).


New Project Request