f9dzyw
Last Updated: February 25, 2016
·
747
· plugin73
Bcdbd4f200c03d452cd1fd0f8853e343

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>
Say Thanks
Respond

2 Responses
Add your response

5137
B3881a28fe402dd2d1de44717486cae8

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 ·
5140
Bcdbd4f200c03d452cd1fd0f8853e343

@citizen428 Of course, I'm inconsiderate ...
Thank you!

over 1 year ago ·
Awesome Job

0957a9c2 0855 11e8 909a 82469fe19f14
Senior Backend Engineer
·
San Francisco, California
·
Full Time