ChatGPT解决这个技术问题 Extra ChatGPT

Ruby中“and”和&&的区别?

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


t
the Tin Man

and&& 相同,但带有 lower precedence。他们都使用 short-circuit evaluation

警告:and 的优先级甚至低于 =,因此您通常希望避免使用 and。应该使用 and 的示例可以在 Rails 指南中的“Avoiding Double Render Errors”下找到。


最好指定通常应使用 &&,而 and 应仅用于非常特殊的情况。
这里还有一个很好的解释:devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby
来自 Andrew Marshall 的链接:“关于 and 的另一种思考方式是反向的 if 语句修饰符:next if widget = widgets.pop 变为 widget = widgets.pop and next。这是一种很好的表达方式,真的让它在我脑海中“点击”。 (并且 or 就像一个反转的 unless 修饰符。)
将此答案与 tadman 答案的详细信息结合起来,您就可以了解整个情况。
Avdi 更新了他对何时使用和 vs. &&. 的看法。基本上使用'and'和'or'作为控制流,因为它们的优先级较低。 devblog.avdi.org/2014/08/26/…
A
Andrew Marshall

实际的区别是结合强度,如果你没有准备好,它可能会导致特殊的行为:

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

||or 同样适用。


a = foo and bar and (a = foo ) && bar 证明 and 的优先级低于 &&
我不明白:“foo and bar”是什么意思?
a = foo and bar 等同于 (a = :foo) and nil。由于赋值返回一个逻辑真值 (:foo),因此第二部分计算失败,返回 nil
A
Andrew Grimm

Ruby Style Guide 说得比我好:

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

# boolean expression
if some_condition && some_other_condition
  do_something
end

# control flow
document.saved? or document.save!

实际上 guide now says 完全避免了 and/or,他们可能有一点。通常它们在控制流 could be more obviously written with if/unless operators 中的使用(例如 document.save! unless document.saved?
@akostadinov,以防您不是在拖钓:Ruby 风格指南不是由 Ruby 的创建者编写的。 Ruby 由 Yukihiro Matsumoto 和其他人创建,而 Ruby 样式指南是由 Bozhidar Batsov mainly
@AndrewGrimm,谢谢,很高兴知道。很抱歉拖钓,但我真的对红宝石现实的某些方面感到困惑。有一件事是肯定的——每个 ruby 项目都需要严格的样式策略来保持代码库的可维护性。
G
Gabe Kopley

||&& 与编程语言中布尔运算符的优先级绑定(&& 非常强,|| 稍弱)。

andor 的优先级较低。

例如,与 || 不同,or 的优先级低于 =

> a = false || true
 => true 
> a
 => true 
> a = false or true
 => true 
> a
 => false

同样,与 && 不同,and 的优先级也低于 =

> a = true && false
 => false 
> a
 => false 
> a = true and false
 => false 
> a
 => true 

此外,与 &&|| 不同的是,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

弱绑定 andor 可能对控制流有用:参见 http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/


“与 || 不同,or 的优先级低于 =” ...现在更有意义了,谢谢!
t
the Tin Man

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

谢谢,但是“and”的优先级与“&&”有什么不同?
@BKSpureon 有关 Ruby 中运算符优先级的有序列表,请参阅 here
t
the Tin Man

and 的优先级较低,通常我们将其用作控制流修饰符,例如 if

next if widget = widgets.pop

变成

widget = widgets.pop and next

对于 or

raise "Not ready!" unless ready_to_rock?

变成

ready_to_rock? or raise "Not ready!"

我更喜欢使用 if 而不是 and,因为 if 更容易理解,所以我忽略了 andor

有关详细信息,请参阅“Using “and” and “or” in Ruby”。


K
Kevin Ng

我不知道这是 Ruby 的意图还是这是一个错误,但请尝试下面的代码。此代码在 Ruby 版本 2.5.1 上运行,并且在 Linux 系统上。

puts 1 > -1 and 257 < 256
# => false

puts 1 > -1 && 257 < 256
# => true

@JakubArnold 讽刺从来没有帮助。例子有时是。
@BobRodes 这不是讽刺。有7个答案,其中6个已经有例子。
@JakubArnold 我仍然觉得这个例子很有帮助。
我也得到了奇怪的结果。 v1 = true and false p v1 # => app.rb: true, IRB: false v2 = true && false p v2 # => app.rb: false, IRB: false puts 1 > -1 && 257 < 256 # => app.rb:假,IRB:假
D
Dheeraj Maheshwari

并且只检查第一个条件并给出结果 && 强烈地检查两个条件并给出逻辑结果。