Skip to content

Instantly share code, notes, and snippets.

@cnosuke
Created October 9, 2013 04:43
Show Gist options
  • Save cnosuke/6896318 to your computer and use it in GitHub Desktop.
Save cnosuke/6896318 to your computer and use it in GitHub Desktop.
Object#try and NilClass#try
class Object
def try(*a, &b)
if a.empty? && block_given?
yield self
else
public_send(*a, &b) if respond_to?(a.first)
end
end
end
class NilClass
def try(*args)
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment