新手问题 what ‘s the meaning of "try"

xiaoronglv · August 09, 2012 · Last by knwang replied at August 10, 2012 · 2048 hits

在看 terry 的视频,关于 omniauth 的第三方验证。

http://railscasts-china.com/episodes/3-omniauth-1

此处的 try 是什么意思,搜索后无果,所以请大家指点一下,灰常感谢。

rails 的 api 中找找

“尝试”调用 find_by_provider_and_uid 的 user 方法,如果没有也不用报错。

简单化的说不考虑 block,

object.try(:user) 相当于 object && object.user

是为了防着 nil. 如果只是 object.user 一旦 object 为 nil 就要出错。其实用这个也算无奈,最好还是应该提早处理 nil, 不要让 nil 溜的很远

You need to Sign in before reply, if you don't have an account, please Sign up first.