aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRichard Schneeman <richard.schneeman+no-recruiters@gmail.com>2018-08-29 09:10:49 -0700
committerGitHub <noreply@github.com>2018-08-29 09:10:49 -0700
commit96d7504da9273e8fcd18823173537678f4cf2321 (patch)
tree2719c0a3df2a6fb455179141d58e85aa3f5b43a4 /activesupport
parenta64dba1dc59efec39080b8e142894060e2e8d0bb (diff)
parentba7d1265e3f2755f55243f32c0264c5c20e01610 (diff)
downloadrails-96d7504da9273e8fcd18823173537678f4cf2321.tar.gz
rails-96d7504da9273e8fcd18823173537678f4cf2321.tar.bz2
rails-96d7504da9273e8fcd18823173537678f4cf2321.zip
Merge pull request #33747 from schneems/schneems/faster-try
32% Faster Object#try
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/object/try.rb11
1 files changed, 10 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb
index c874691629..bd841771ce 100644
--- a/activesupport/lib/active_support/core_ext/object/try.rb
+++ b/activesupport/lib/active_support/core_ext/object/try.rb
@@ -5,7 +5,16 @@ require "delegate"
module ActiveSupport
module Tryable #:nodoc:
def try(*a, &b)
- try!(*a, &b) if a.empty? || respond_to?(a.first)
+ return unless a.empty? || respond_to?(a.first)
+ if a.empty? && block_given?
+ if b.arity == 0
+ instance_eval(&b)
+ else
+ yield self
+ end
+ else
+ public_send(*a, &b)
+ end
end
def try!(*a, &b)