Ruby on Rails Friday, November 6, 2015


On Friday, November 6, 2015 at 9:32:21 AM UTC, Marco Antonio Almeida wrote:

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.


A gotcha is that if quote_request is non nil, but doesn't actually have a method called status then try will ignore the NoMethodError and just return nil ( try! on the other hand will raise in this case). Prior to rails 4, try behaved like try!

Fred 

--
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/61d753e4-213b-4711-8d8e-b34290efb361%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment