1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix and improve the method signature for try and try!

Expand `try(*a, &b)` to `try(*args, &block)`, and fix invalid `a*`.
This commit is contained in:
Ryuta Kamizono 2021-05-03 03:31:27 +09:00
parent ab13f9549d
commit 917e81a808

View file

@ -4,28 +4,28 @@ require "delegate"
module ActiveSupport module ActiveSupport
module Tryable #:nodoc: module Tryable #:nodoc:
def try(method_name = nil, *args, &b) def try(method_name = nil, *args, &block)
if method_name.nil? && block_given? if method_name.nil? && block_given?
if b.arity == 0 if block.arity == 0
instance_eval(&b) instance_eval(&block)
else else
yield self yield self
end end
elsif respond_to?(method_name) elsif respond_to?(method_name)
public_send(method_name, *args, &b) public_send(method_name, *args, &block)
end end
end end
ruby2_keywords(:try) ruby2_keywords(:try)
def try!(method_name = nil, *args, &b) def try!(method_name = nil, *args, &block)
if method_name.nil? && block_given? if method_name.nil? && block_given?
if b.arity == 0 if block.arity == 0
instance_eval(&b) instance_eval(&block)
else else
yield self yield self
end end
else else
public_send(method_name, *args, &b) public_send(method_name, *args, &block)
end end
end end
ruby2_keywords(:try!) ruby2_keywords(:try!)
@ -39,7 +39,7 @@ class Object
# :method: try # :method: try
# #
# :call-seq: # :call-seq:
# try(*a, &b) # try(*args, &block)
# #
# Invokes the public method whose name goes as first argument just like # Invokes the public method whose name goes as first argument just like
# +public_send+ does, except that if the receiver does not respond to it the # +public_send+ does, except that if the receiver does not respond to it the
@ -104,7 +104,7 @@ class Object
# :method: try! # :method: try!
# #
# :call-seq: # :call-seq:
# try!(*a, &b) # try!(*args, &block)
# #
# Same as #try, but raises a +NoMethodError+ exception if the receiver is # Same as #try, but raises a +NoMethodError+ exception if the receiver is
# not +nil+ and does not implement the tried method. # not +nil+ and does not implement the tried method.
@ -121,7 +121,7 @@ class Delegator
# :method: try # :method: try
# #
# :call-seq: # :call-seq:
# try(a*, &b) # try(*args, &block)
# #
# See Object#try # See Object#try
@ -129,7 +129,7 @@ class Delegator
# :method: try! # :method: try!
# #
# :call-seq: # :call-seq:
# try!(a*, &b) # try!(*args, &block)
# #
# See Object#try! # See Object#try!
end end