aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Schneeman <richard.schneeman+no-recruiters@gmail.com>2018-08-29 12:22:20 -0700
committerGitHub <noreply@github.com>2018-08-29 12:22:20 -0700
commit3d2caab7dc92a13d4dd369678d5b4ce659df8e52 (patch)
tree1b9095c945ea3c1dffcb3133be22ca372f06350b
parentaab0c25f582616f061c796df3e42a9e966bce88c (diff)
parent0f462da85dbd7b6ad6c1d79858b8aa6f28ca9638 (diff)
downloadrails-3d2caab7dc92a13d4dd369678d5b4ce659df8e52.tar.gz
rails-3d2caab7dc92a13d4dd369678d5b4ce659df8e52.tar.bz2
rails-3d2caab7dc92a13d4dd369678d5b4ce659df8e52.zip
Merge pull request #33752 from sgrif/sg-faster-try
20% faster `try`
-rw-r--r--activesupport/lib/active_support/core_ext/object/try.rb15
1 files changed, 7 insertions, 8 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb
index bd841771ce..aa6896af32 100644
--- a/activesupport/lib/active_support/core_ext/object/try.rb
+++ b/activesupport/lib/active_support/core_ext/object/try.rb
@@ -4,28 +4,27 @@ require "delegate"
module ActiveSupport
module Tryable #:nodoc:
- def try(*a, &b)
- return unless a.empty? || respond_to?(a.first)
- if a.empty? && block_given?
+ def try(method_name = nil, *args, &b)
+ if method_name.nil? && block_given?
if b.arity == 0
instance_eval(&b)
else
yield self
end
- else
- public_send(*a, &b)
+ elsif respond_to?(method_name)
+ public_send(method_name, *args, &b)
end
end
- def try!(*a, &b)
- if a.empty? && block_given?
+ def try!(method_name = nil, *args, &b)
+ if method_name.nil? && block_given?
if b.arity == 0
instance_eval(&b)
else
yield self
end
else
- public_send(*a, &b)
+ public_send(method_name, *args, &b)
end
end
end