“a &&而不是b”会产生语法错误吗?

时间:2021-05-08 15:42:07

This line gives me a syntax error:

这一行给了我一个语法错误:

if @array.include?('cat') && not @array.include?('dog')

如果@array.include?('cat') &而不是@array.include?('dog')

Any ideas?

什么好主意吗?

1 个解决方案

#1


6  

Do this:

这样做:

if @array.include?('cat') && ! @array.include?('dog')

! and not do mostly the same thing but can't quite be used interchangeably.

!而不是主要做同样的事情,但不能互换使用。

For the curious: There is actually some sort of quirk in the ruby parser that makes it unable to interpret expressions like this one, even though they are theoretically parseable and sound.

让人好奇的是:ruby解析器中实际上存在某种怪癖,使它无法解释像这样的表达式,尽管它们理论上是可解析的,而且是合理的。

These can be parsed:

这些可以解析:

not true && true
true && not(true)

but these cannot:

但是这些不能:

true && not true
true && not (true)

Note that last one is different only in the extra space before (!

注意,最后一个是不同的只有在额外的空间之前(!

#1


6  

Do this:

这样做:

if @array.include?('cat') && ! @array.include?('dog')

! and not do mostly the same thing but can't quite be used interchangeably.

!而不是主要做同样的事情,但不能互换使用。

For the curious: There is actually some sort of quirk in the ruby parser that makes it unable to interpret expressions like this one, even though they are theoretically parseable and sound.

让人好奇的是:ruby解析器中实际上存在某种怪癖,使它无法解释像这样的表达式,尽管它们理论上是可解析的,而且是合理的。

These can be parsed:

这些可以解析:

not true && true
true && not(true)

but these cannot:

但是这些不能:

true && not true
true && not (true)

Note that last one is different only in the extra space before (!

注意,最后一个是不同的只有在额外的空间之前(!