jpablobr.com

→ Rails Object.try

More on: Rails, Ruby, Fri Jul 16 18:38:15 +0000 2010

Object.try is an ActiveSupport method which lets you attempt to invoke a method on an object without worrying about a NoMethodError being raised.

Example:

file_type = file_types.detect { |key,| name.include? key }.try(:last)

Nice!

blog comments powered by Disqus