try first, question objects later

Sometimes you come across an idea that is so obvious, you can’t believe you aren’t already using it. I recently saw a post on [try](http://ozmm.org/posts/try.html “try() — ones zeros majors and minors”), it is a neat little method that lets you do:

Person.find(:first, :conditions =>
    some_conditions).try(:destroy)

instead of:

person = Person.find(:first, :conditions =>
    some_conditions)
person.destroy unless person.nil?

Personally, I think that using **try** makes the code more expressive. It is very easy to include in your source code, you just need to open **Object** somewhere. Here is the code:

class Object
  ##
  #   @person ? @person.name : nil
  # vs
  #   @person.try(:name)
  def try(*args)
    send *args if respond_to? args[0]
  end
end

I modified it a little from the original to handle multiple arguments, if you want the original you can grab it from the link above.

Feeds/Syndication