Skip to content
This repository
Browse code

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

…nted at http://is.gd/hdmo and http://is.gd/hdmQ

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 6df580a0a8fe9c5df2353e5b435590a9a1cab3de 1 parent c9b8a26
Christos Zisopoulos christos authored lifo committed

Showing 1 changed file with 9 additions and 4 deletions. Show diff stats Hide diff stats

  1. +9 4 activesupport/lib/active_support/core_ext/try.rb
13 activesupport/lib/active_support/core_ext/try.rb
... ... @@ -1,6 +1,9 @@
1 1 class Object
2   - # Tries to send the method only if object responds to it. Return +nil+ otherwise.
3   - # It will also forward any arguments and/or block like Object#send does.
  2 + # Invokes the method identified by +symbol+, passing it any arguments
  3 + # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
  4 + #
  5 + # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
  6 + # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
4 7 #
5 8 # ==== Examples
6 9 #
@@ -12,11 +15,13 @@ class Object
12 15 # With try
13 16 # @person.try(:name)
14 17 #
15   - # Try also accepts arguments/blocks for the method it is trying
  18 + # +try+ also accepts arguments and/or a block, for the method it is trying
16 19 # Person.try(:find, 1)
17 20 # @people.try(:collect) {|p| p.name}
18 21 #--
19   - # This method def is for rdoc only. The alias_method below overrides it as an optimization.
  22 + # This method definition below is for rdoc purposes only. The alias_method call
  23 + # below overrides it as an optimization since +try+ behaves like +Object#send+,
  24 + # unless called on +NilClass+.
20 25 def try(method, *args, &block)
21 26 send(method, *args, &block)
22 27 end

0 comments on commit 6df580a

Please sign in to comment.
Something went wrong with that request. Please try again.