Good notes posted by mavvie

RSS feed
August 21, 2014 - (v4.0.2)
3 thanks

In Rails 4 it DOES return nil, even if the object you try from isn't nil

The try method does not raise a NoMethodError if you call a method that doesn’t exist on an object.

a = Article.new

a.try(:author) #=> #<Author ...>

nil.try(:doesnt_exist) #=> nil

a.try(:doesnt_exist) #=> nil


a.try(&:doesnt_exist) #=> Raises NoMethodError