If in doubt, try try again

If in doubt, try try agin

On some recent projects I have seen the following code similar to:


User.find_by_login("noone").id rescue nil

So I just wanted to remind people of the try method.


User.find_by_login("noone").try(:id)

Personally I think try communicates the intent of the code.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">