Skip to content

Commit 4a906cb

Browse files
authored
Merge pull request rails#42796 from schneems/schneems/faster-try-yet-again
Fix NilClass#try and NilClass#try! performance slowdown in Ruby 2.7+
2 parents 675d9ff + bb513b8 commit 4a906cb

File tree

1 file changed

+9
-9
lines changed
  • activesupport/lib/active_support/core_ext/object

1 file changed

+9
-9
lines changed

activesupport/lib/active_support/core_ext/object/try.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44

55
module ActiveSupport
66
module Tryable #:nodoc:
7-
def try(method_name = nil, *args, &block)
8-
if method_name.nil? && block_given?
7+
def try(*args, &block)
8+
if args.empty? && block_given?
99
if block.arity == 0
1010
instance_eval(&block)
1111
else
1212
yield self
1313
end
14-
elsif respond_to?(method_name)
15-
public_send(method_name, *args, &block)
14+
elsif respond_to?(args.first)
15+
public_send(*args, &block)
1616
end
1717
end
1818
ruby2_keywords(:try)
1919

20-
def try!(method_name = nil, *args, &block)
21-
if method_name.nil? && block_given?
20+
def try!(*args, &block)
21+
if args.empty? && block_given?
2222
if block.arity == 0
2323
instance_eval(&block)
2424
else
2525
yield self
2626
end
2727
else
28-
public_send(method_name, *args, &block)
28+
public_send(*args, &block)
2929
end
3030
end
3131
ruby2_keywords(:try!)
@@ -145,14 +145,14 @@ class NilClass
145145
#
146146
# With +try+
147147
# @person.try(:children).try(:first).try(:name)
148-
def try(_method_name = nil, *)
148+
def try(*)
149149
nil
150150
end
151151

152152
# Calling +try!+ on +nil+ always returns +nil+.
153153
#
154154
# nil.try!(:name) # => nil
155-
def try!(_method_name = nil, *)
155+
def try!(*)
156156
nil
157157
end
158158
end

0 commit comments

Comments
 (0)