我有这样的声明:
if user = params[:user] && action == :delete
end
为什么用户总是设置为:删除?
如果用户通过,则不应将其设置为用户名,否则不应设置为 nil。
我有这样的声明:
if user = params[:user] && action == :delete
end
为什么用户总是设置为:删除?
如果用户通过,则不应将其设置为用户名,否则不应设置为 nil。
您需要()
在 Ruby 中放置作业,以便按照您的预期对其进行评估
if (user = params[:user]) && action == :delete
end
&&
绑定比assingment强。and
如果您热衷于省略括号,则可以改用:
if user = params[:user] and action == :delete
end
作为风格问题- 使用 = (赋值)的返回值是可以的,但用括号括住赋值。
# good - shows intended use of assignment
if (v = array.grep(/foo/)) ...
# bad
if v = array.grep(/foo/) ...
# also good - shows intended use of assignment and has correct precedence.
if (v = self.next_value) == 'hello' ...
不鼓励在条件表达式中使用and
and ,因此最好使用 Ian Bishop 建议的解决方案。or