Call method only if it exists
If you are not satisfied with the standard ruby syntax for that, you are free to:
class Object
def try_outside_rails(meth, *args, &cb)
self.send(meth.to_sym, *args, &cb) if self.respond_to?(meth.to_sym)
end
end
Now:
resource.try_outside_rails(:phone_number)
will behave as you wanted.
I would try defined?
(http://ruby-doc.org/docs/keywords/1.9/Object.html#defined-3F-method). It seems to do exactly what you are asking for:
resource.phone_number if defined? resource.phone_number
I know this is very old post. But just wanted to know if this could be a possible answer and whether the impact is the same .
resource.try(:phone_number) rescue nil
Thanks