Best Practice: Try vs Rescue

前端 未结 3 936
清歌不尽
清歌不尽 2021-01-30 02:43

What is a best practice? To use try or use rescue?

user.try(:email)

VS

user.email rescue nil
<         


        
3条回答
  •  庸人自扰
    2021-01-30 03:34

    Both seem fishy and can mask other bugs. Are you sure you really want to get nil there? Maybe it would be better to check whether there are any comments first, and cover the empty case explicitly?

提交回复
热议问题