diff options
author | Eloy Duran <eloy.de.enige@gmail.com> | 2008-11-21 09:47:55 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-11-24 18:53:48 +0100 |
commit | 823b623fe2de8846c37aa13250010809ac940b57 (patch) | |
tree | 7bf0e7e557bdedb57226cd35fef12f7e9fbbea4b /activesupport/lib/active_support/core_ext/object/misc.rb | |
parent | fffb1da3f22852c722dbc4436ec7c924435d29a5 (diff) | |
download | rails-823b623fe2de8846c37aa13250010809ac940b57.tar.gz rails-823b623fe2de8846c37aa13250010809ac940b57.tar.bz2 rails-823b623fe2de8846c37aa13250010809ac940b57.zip |
Allow optional arguments and/or block for Object#try like Object#send does. [#1425 state:resolved]
Original suggestion by Pat Nakajima.
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support/core_ext/object/misc.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/misc.rb | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index 50f824c358..46f9c7d676 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -73,6 +73,7 @@ class Object end # Tries to send the method only if object responds to it. Return +nil+ otherwise. + # It will also forward any arguments and/or block like Object#send does. # # ==== Example : # @@ -81,7 +82,11 @@ class Object # # With try # @person.try(:name) - def try(method) - send(method) if respond_to?(method, true) + # + # # try also accepts arguments/blocks for the method it is trying + # Person.try(:find, 1) + # @people.try(:map) {|p| p.name} + def try(method, *args, &block) + send(method, *args, &block) if respond_to?(method, true) end end |