Ruby中"和"与&&的区别?

Jak*_*old 303 ruby operators

Ruby中的&&and运算符有什么区别?

Dom*_*ger 335

and是相同&&优先级较低.他们都使用短路评估.

警告:and即使优先级低于=and始终要避免的优先级

  • 指定一个应该通常使用`&&`是一个好主意,而`和`应该只用于非常特殊的情况. (47认同)
  • 来自Andrew Marshall的链接:"另一种思考`和`的方式是作为一个反向的`if`语句修饰符:`next,如果widget = widgets.pop`变成`widget = widgets.pop和next`.这是一个很好的放置方式它真的让我在脑海中"点击".(和`或`就像一个反向的`除非`修饰符.) (15认同)
  • 另一个很好的解释:http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/. (10认同)
  • Avdi更新了他对何时使用和对&&的看法.基本上使用'和'和'或'作为控制流,因为它们的优先级较低.http://devblog.avdi.org/2014/08/26/how-to-use-rubys-english-andor-operators-without-going-nuts/ (4认同)

tad*_*man 236

实际差异是绑定力量,如果你没有做好准备,可能导致特殊的行为:

foo = :foo
bar = nil

a = foo and bar
# => nil
a
# => :foo

a = foo && bar
# => nil
a
# => nil

a = (foo and bar)
# => nil
a
# => nil

(a = foo) && bar
# => nil
a
# => :foo
Run Code Online (Sandbox Code Playgroud)

同样适用于||or.

  • `a = foo and bar` _and_`(a = foo)&& bar`证明`和`的优先级低于`&&`. (2认同)

And*_*imm 59

Ruby的风格指南说,它比我更可以:

使用&&/|| 用于布尔表达式和/或用于控制流.(经验法则:如果必须使用外括号,则使用错误的运算符.)

# boolean expression
if some_condition && some_other_condition
  do_something
end

# control flow
document.saved? or document.save!
Run Code Online (Sandbox Code Playgroud)

  • 实际上[**指南现在说**](https://github.com/bbatsov/ruby-style-guide/commit/5920497452c1f6f604742a735f5684e86d4c0003)以避免`和`/`或'完全,他们可能有一个点.通常它们在控制流中的使用[可以更明显地使用`if` /`除非`运算符编写](http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby /)无论如何(例如`document.save!除非document.saved?`) (49认同)
  • @AndrewGrimm,谢谢,很高兴知道。很抱歉拖钓,但是我对红宝石现实的某些方面感到非常困惑。可以肯定的是-每个ruby项目都需要严格的样式策略来保持代码库的可维护性。 (2认同)

Gab*_*ley 37

||&&使用您期望从编程语言中的布尔运算符的优先级绑定(&&非常强大,||稍微强一点).

and并且or优先级较低.

例如,不像||, or优先级低于=:

> a = false || true
 => true 
> a
 => true 
> a = false or true
 => true 
> a
 => false
Run Code Online (Sandbox Code Playgroud)

同样,与之相比&&,and优先级也低于=:

> a = true && false
 => false 
> a
 => false 
> a = true and false
 => false 
> a
 => true 
Run Code Online (Sandbox Code Playgroud)

更重要的是,不像&&||,andor绑定与相同的优先级:

> !puts(1) || !puts(2) && !puts(3)
1
 => true
> !puts(1) or !puts(2) and !puts(3)
1
3
 => true 
> !puts(1) or (!puts(2) and !puts(3))
1
 => true
Run Code Online (Sandbox Code Playgroud)

弱结合andor可以是用于控制流目的是有用的:见http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/.

  • "不像`||`,`或`的优先级低于`=`"...现在它更有意义,谢谢! (2认同)

San*_*osh 18

and优先级低于&&.

但对于一个不起眼的用户,如果它与其中优先级介于两者之间的其他运算符一起使用,则可能会出现问题,例如赋值运算符.

例如

def happy?() true; end
def know_it?() true; end

todo = happy? && know_it? ? "Clap your hands" : "Do Nothing"

todo
# => "Clap your hands"

todo = happy? and know_it? ? "Clap your hands" : "Do Nothing"

todo
# => true
Run Code Online (Sandbox Code Playgroud)

  • @BKSpurgeon参见[here](https://ruby-doc.org/core-2.4.1/doc/syntax/precedence_rdoc.html)获取Ruby中运算符优先级的有序列表. (2认同)

Feu*_*uda 5

并且具有较低的优先级,大多数我们将其用作控制流修饰符,例如if

next if widget = widgets.pop
Run Code Online (Sandbox Code Playgroud)

widget = widgets.pop and next
Run Code Online (Sandbox Code Playgroud)

raise "Not ready!" unless ready_to_rock?
Run Code Online (Sandbox Code Playgroud)

ready_to_rock? or raise "Not ready!"
Run Code Online (Sandbox Code Playgroud)

我更喜欢使用,如果但不,因为如果是更容易理解,所以我只是忽视.

参考

在Ruby中使用"和"和"或"