On 6 November 2015 at 09:31, Marco Antonio Almeida <[email protected]> wrote: > On Fri, Nov 6, 2015 at 10:03 AM Colin Law <[email protected]> wrote: >> ... >> How about >> def rqstate >> (quote_request && quote_request.status) ? quote_request.status : >> "unsubmitted" >> end > > > If you're using Rails, another approach is to use try > (http://apidock.com/rails/Object/try). > > def rqstate > quote_request.try(:status) || "unsubmitted" > end > > #try is very nice in this case, but avoid overdoing it. I have faced many > codebases with tons of try methods chained and then you lose code > readability.
Personally I don't like try. I don't find it easy to intuitively understand what it is doing, I always seem to have to think about the code to work out what it is doing. Perhaps that is just my old brain getting past it though. Of course the ? : syntax can easily become opaque in more complex cases also. Colin > >> >> Not only a one liner but I think easier to understand. Assuming I >> have got it right :) >> >> Colin >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtLLog2%2B3B0EV0gy4jwQLCt5FgUzBKZBRLRmmh95JEJQg%40mail.gmail.com. >> For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/CACMkcE41cpqhNK-W9Y9XP2AummBfabOTQguTEtR2WmopR%2B3M%3Dg%40mail.gmail.com. > > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLudjvzQfXmirtKh-gCGPwiiE%3DK4kmKZSEZXRZ0zN59R6Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

