aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorGodfrey Chan <godfreykfc@gmail.com>2014-10-23 19:05:46 -0700
committerGodfrey Chan <godfreykfc@gmail.com>2014-10-23 19:05:46 -0700
commit016af48b86f2096637f39c3194402d9891f35dcd (patch)
tree6d8fc3fcba66ea63359373c8591a60dc204fa56d /activesupport
parent1d9ebec0a9e84aa680313b17ceb800f1b10df3b9 (diff)
parent22f6189a55b3a096e91d3ec3a97cf5114fc93b72 (diff)
downloadrails-016af48b86f2096637f39c3194402d9891f35dcd.tar.gz
rails-016af48b86f2096637f39c3194402d9891f35dcd.tar.bz2
rails-016af48b86f2096637f39c3194402d9891f35dcd.zip
Merge pull request #17377 from aripollak/dry-try-bang
DRY up try/try!
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/object/try.rb16
1 files changed, 6 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb
index 112ec05e15..09b8a19789 100644
--- a/activesupport/lib/active_support/core_ext/object/try.rb
+++ b/activesupport/lib/active_support/core_ext/object/try.rb
@@ -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