Method: NilClass#try

Defined in:
lib/active_support/core_ext/object/try.rb

#tryObject

Calling try on nil always returns nil. It becomes especially helpful when navigating through associations that may return nil.

nil.try(:name) # => nil

Without try

@person && @person.children.any? && @person.children.first.name

With try

@person.try(:children).try(:first).try(:name)


148
149
150
# File 'lib/active_support/core_ext/object/try.rb', line 148

def try(*)
  nil
end