Note: I am not exactly sure what to name the question, so if someone has a better idea please edit it.
I will jump right into the question, since there isn’t any fore-explaining required.
This code:
!foo = true
generates this warning
warning: found = in conditional, should be ==
I would understand if this was happening after an if or unless statement, but this couldn’t be further away from them (exaggerating). I do realise I could use:
foo = true
!foo
I suppose, the warning isn’t a big deal, but it is a bit irritating that Ruby is assuming I am doing something wrong—when I am not.
Questions:
- Is this a bug?
- Can the warning be disabled?
Thanks!
Is legal. Not a bug. The warning can be suppressed.
You can disable the warning with:
It’s interesting,
$VERBOSEis a case where setting something tofalsedoes something different than setting it tonil.By the way, the other answers, at least initially, tend to assume that Ruby parses the expression as
… but that’s not the case. It is parsed as:
… and so it’s doing exactly what the OP wanted. And there is no specification or ratified standard for Ruby, so if it works in MRI (the reference implementation) then it’s legal.