2

可能重复:
Ruby 中“and”和 && 的区别?

and我意识到 Ruby和Ruby之间存在差异&&(这是一个令人困惑的句子)。

@show_votes = !current_user.nil? and current_user.is_admin?
if @show_votes
  #code assumes everyone logged in is admin
end

然而:

@show_votes = !current_user.nil? && current_user.is_admin?
if @show_votes
  #code assumes only admin is admin
end

这两个运营商有什么区别?我发现它们有不同的操作优先级,但仍然无法弄清楚真正的区别是什么。

(该应用程序是用 Rails 编写的。虽然我希望在 Sinatra 中也会发生同样的情况,因为这很可能与 Ruby 语言有关)

编辑代码以更好地反映情况。

4

0 回答 0