Class: NilClass

Inherits:
Object show all
Includes:
RubyTry
Defined in:
lib/ruby-try.rb

Instance Method Summary collapse

Methods included from RubyTry

#try?

Instance Method Details

#try(*args) ⇒ Object

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)


88
89
90
# File 'lib/ruby-try.rb', line 88

def try(*args)
  nil
end

#try!(*args) ⇒ Object



92
93
94
# File 'lib/ruby-try.rb', line 92

def try!(*args)
  nil
end