Browse files

An updated description of Object#try to better reflect changes docume…

  • Loading branch information...
1 parent 68ad154 commit 929088189de1d189c643246a491ba8fb5347b1d3 Christos Zisopoulos committed Jan 26, 2009
Showing with 11 additions and 6 deletions.
  1. +11 −6 activesupport/lib/active_support/core_ext/try.rb
View
17 activesupport/lib/active_support/core_ext/try.rb
@@ -1,6 +1,9 @@
class Object
- # 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.
+ # Invokes the method identified by +symbol+, passing it any arguments
+ # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
+ #
+ # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
+ # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
#
# ==== Examples
#
@@ -12,13 +15,15 @@ class Object
# With try
# @person.try(:name)
#
- # Try also accepts arguments/blocks for the method it is trying
+ # +try+ also accepts arguments and/or a block, for the method it is trying
# Person.try(:find, 1)
# @people.try(:collect) {|p| p.name}
#--
- # This method def is for rdoc only. The alias_method below overrides it as an optimization.
- def try(method, *args, &block)
- send(method, *args, &block)
+ # This method definition below is for rdoc purposes only. The alias_method call
+ # below overrides it as an optimization since +try+ behaves like +Object#send+,
+ # unless called on +NilClass+.
+ def try(symbol, *args, &block)
+ send(symbol, *args, &block)
end
alias_method :try, :__send__
end

0 comments on commit 9290881

Please sign in to comment.