Permalink
Browse files

Docs for NilClass#try

  • Loading branch information...
1 parent 0675047 commit bb626e785a9ad32d025e429af24654f2af662d09 @smartinez87 smartinez87 committed Apr 17, 2011
Showing with 13 additions and 1 deletion.
  1. +13 −1 activesupport/lib/active_support/core_ext/object/try.rb
@@ -34,7 +34,19 @@ def try(*a, &b)
end
end
-class NilClass #:nodoc:
+class NilClass
+ # Instances of NilClass return always +nil+
+ # It becomes specially helpful when navigating through associations that may return nil
+ #
+ # === Examples
+ #
+ # nil.try(:name) => nil
+ #
+ # Without try
+ # @person && @person.children.first && @person.children.first.name
+ #
+ # With try
+ # @person.try(:children).try(:first).try(:name)
def try(*args)
nil
end

0 comments on commit bb626e7

Please sign in to comment.