class NilClass

Public Instance Methods

try(*args) click to toggle source

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

nil.try(:name) # => nil

Without try

@person && !@person.children.blank? && @person.children.first.name

With try

@person.try(:children).try(:first).try(:name)
# File lib/ruby-try.rb, line 88
def try(*args)
  nil
end
try!(*args) click to toggle source
# File lib/ruby-try.rb, line 92
def try!(*args)
  nil
end