f9dzyw
Last Updated: February 25, 2016
·
762
· plugin73

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>

2 Responses
Add your response

5137

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

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

over 1 year ago ·