Last Updated: February 25, 2016
·
868
· 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

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 ·