Object#try is a solution that I like. However, it uses Object#respond_to?
Ruby Community uses this solution recently in github.
class Object
##
# @person ? @person.name : nil
# vs
# @person.try(:name)
def try(method)
send method if respond_to? method
end
end
But it does not solve the problem of args and blocks.
The right solution for generic try method is :
#By Jagan Reddy
class Object
def try(method, *args, &block)
send(method, *args, &block) if respond_to?(method, true)
end
end