I came to know about Ruby's try() method today, liked, the overall purpose of this method and so, sharing a brief note about how we can use this method while writing codes.
Many a times, we have to write conditions for checking for nil values/objects. try() can help us to write clean codes whereby we don't have explicitly do testing for nil value.
Have a look at the small example below
Without try():
puts @student.school.name if @student.school
With try():
puts @student.school.try(:name)
No comments:
Post a Comment