Josh W Lewis
Josh W Lewis
/
Essays
/
Rails' ActiveSupport try

Rails' ActiveSupport try


By on April 26, 2013


ActiveSupport has a few great Ruby extensions that I just love. One of my favorites is try.

.try()

Try is a sneaky method for handling NoMethodErrors on NilClass. If you ever get stuck writing code like this:

name = user.name unless user.nil?

Consider using try instead:

name = user.try :name

It’s basically like using send, but it will return nil if the receiver is nil.

Here it works like send:

user = User.new name: "Josh"    #=> #<User>
user.send :name                 #=> "Josh"
user.try :name                  #=> "Josh"

And here it doesn’t blow up with a NoMethodError:

user = nil                      #=> nil
user.send :name                 #=> NoMethodError
user.try :name                  #=> nil

EDIT: I still find try helpful in some cases, but I think it’s best to exercise caution with it. Many times delegation or the null object pattern can solve your problem a little more eloquently. If you find yourself chaining try you should probably look at another way to factor your code.

This article was categorized as rails and ruby