相关疑难解决方法(0)

最佳实践:尝试与救援

什么是最佳做法?使用try或使用rescue

user.try(:email)
Run Code Online (Sandbox Code Playgroud)

VS

user.email rescue nil
Run Code Online (Sandbox Code Playgroud)
post.try(:comments).try(:first).try(:author)
Run Code Online (Sandbox Code Playgroud)

VS

post.comments.first.author rescue nil
Run Code Online (Sandbox Code Playgroud)

使用其中任何一个有什么不同吗?

ruby ruby-on-rails

71
推荐指数
2
解决办法
5万
查看次数

标签 统计

ruby ×1

ruby-on-rails ×1