Skip to content
This repository
Browse code

Docs for NilClass#try

  • Loading branch information...
commit bb626e785a9ad32d025e429af24654f2af662d09 1 parent 0675047
Sebastian Martinez authored April 17, 2011
14  activesupport/lib/active_support/core_ext/object/try.rb
@@ -34,7 +34,19 @@ def try(*a, &b)
34 34
   end
35 35
 end
36 36
 
37  
-class NilClass #:nodoc:
  37
+class NilClass
  38
+  # Instances of NilClass return always +nil+
  39
+  # It becomes specially helpful when navigating through associations that may return nil
  40
+  #
  41
+  # === Examples
  42
+  #
  43
+  # nil.try(:name) => nil
  44
+  #
  45
+  # Without try
  46
+  #   @person && @person.children.first && @person.children.first.name
  47
+  #
  48
+  # With try
  49
+  #   @person.try(:children).try(:first).try(:name)
38 50
   def try(*args)
39 51
     nil
40 52
   end

0 notes on commit bb626e7

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