Call methods on object without worrying about that object may be nil
Without try
if @person && @person.name
end
</code></pre>
With try
if @person.try(:name)
end</pre>
Written by Alexander Vagin
Related protips
2 Responses

Object#try is a Rails addition and not available in pure Ruby. Maybe update the tags to reflect that. If you want something similar in Ruby, there's a gem called "andand".
over 1 year ago ·

@citizen428 Of course, I'm inconsiderate ...
Thank you!
over 1 year ago ·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best #Ruby Authors
Sponsored by #native_company# — Learn More
#native_title# #native_desc#