这一行给我一个语法错误:
if @array.include?('cat') && not @array.include?('dog')
有什么想法吗?
最佳答案
这样做:
if @array.include?('cat') && ! @array.include?('dog')
!
和 not
大部分做同样的事情,但不能完全互换使用。
对于好奇:ruby 解析器中实际上存在某种怪癖,使其无法解释像这样的表达式,即使它们在理论上是可解析且合理的。
这些可以被解析:
not true && true
true && not(true)
但这些不能:
true && not true
true && not (true)
请注意,最后一个只是在 (
!
关于ruby - "a && not b"给出语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081712/