Skip to content

Commit

Permalink
Merge pull request #17377 from aripollak/dry-try-bang
Browse files Browse the repository at this point in the history
DRY up try/try!
  • Loading branch information
chancancode committed Oct 24, 2014
2 parents 1d9ebec + 22f6189 commit 016af48
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions activesupport/lib/active_support/core_ext/object/try.rb
Expand Up @@ -44,22 +44,18 @@ class Object
# +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
# delegator itself.
def try(*a, &b)
if a.empty? && block_given?
if b.arity.zero?
instance_eval(&b)
else
yield self
end
else
public_send(*a, &b) if respond_to?(a.first)
end
try!(*a, &b) if a.empty? || respond_to?(a.first)
end

# Same as #try, but will raise a NoMethodError exception if the receiving is not nil and
# does not implement the tried method.
def try!(*a, &b)
if a.empty? && block_given?
try(*a, &b)
if b.arity.zero?
instance_eval(&b)
else
yield self
end
else
public_send(*a, &b)
end
Expand Down

0 comments on commit 016af48

Please sign in to comment.