【发布时间】:2014-03-21 15:46:45
【问题描述】:
如果a = false 和b = 2 有没有简洁的方法来完成这个?仅使用 return a unless b 返回 'nil' 而不是 '2'。
我有
def checkit
return a unless b
b
end
这个语句会调用 b 两次吗?
一个真实的案例是:
def current_user
@current_user ||= authenticate_user
end
def authenticate_user
head :unauthorized unless authenticate_from_cookie
end
def authenticate_from_cookie
.
if user && secure_compare(user.cookie, cookie)
return user
else
return nil
end
end
【问题讨论】:
-
代码和语句对我来说是矛盾的.. :( 你在哪里写 If
a = false和b = 2在你的代码中?
标签: ruby return conditional-statements