Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Docs for NilClass#try

  • Loading branch information...
commit bb626e785a9ad32d025e429af24654f2af662d09 1 parent 0675047
@smartinez87 smartinez87 authored
Showing with 13 additions and 1 deletion.
  1. +13 −1 activesupport/lib/active_support/core_ext/object/try.rb
View
14 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
Please sign in to comment.
Something went wrong with that request. Please try again.