在Ruby中,我们应始终使用“&&","||"而不是“和",“或"除非有特殊情况? [英] In Ruby, should we always use "&&", "||" instead of "and", "or" unless for special situations?
问题描述
的确,在大多数情况下,除非有特殊情况,否则在Ruby中,最好使用&&
,||
而不是and
,or
.
Is it true that in most cases, in Ruby, it is best to use &&
, ||
instead of and
, or
, unless it is some special situations.
我认为Ruby的设计原则之一是尽可能地减少惊喜,因此使用and
或or
实际上会带来一些惊喜...例如and
的优先级没有比or
高,而&&
的优先级高于||
.
I think one of Ruby's design principles is to have least surprises as possible, so using and
, or or
actually have some surprises... such as and
not having a higher precedence than or
, while &&
has a higher precedence than ||
.
因此,我认为在大多数情况下,请使用&&
,||
.在某些特殊情况下,可能需要使用and
,or
,但是我认为,如果将它们与&&
,||
混合使用,迟早您在Ruby中工作的同事可能会创建bug.很久以前需要编辑您的代码.
So I think in most cases, use &&
, ||
. In know in some special situations, it may require using and
, or
, but I think if those are intermixed with &&
, ||
, sooner or later it may create bugs when your coworkers who started in Ruby not so long ago need to edit your code.
推荐答案
是.依靠and
和or
进行布尔逻辑是将微妙的bug引入应用程序的好方法.
Yes. Relying on and
and or
for boolean logic is a good way to introduce subtle bugs into your application.
不过,他们确实有地方.当用作控制流运算符时,它们是一种安全且易读的选项.
They do have a place, though. They are a safe and readable option when used as control flow operators.
redirect_to root_url and return
直到我阅读 查看全文