Class: NilClass
Instance Method Summary collapse
-
#try(*args) ⇒ Object
Calling
try
onnil
always returnsnil
. - #try!(*args) ⇒ Object
Methods included from RubyTry
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 |