Dave B. wrote:
“When in doubt, use parentheses.” This avoids having to learn
Matt H. wrote:
It would be my guess ruby is trying to do “a << rand” and then finds
an extra integer stuck on the end.
No doubt. But could anybody describe this special case more accurately
so I recognize it in the future? It even happens for “2 + rand 3”. I
don’t want to add parentheses in all my function calls, because I
understand that’s contrary to Ruby’s coding style.
I had a peek at a simplified BNF of ruby:
and I see:
ARG : …
<<' ARG | ... | ARG+’ ARG
-' ARG | ARG*’ ARG
| ARG `/’ ARG
PRIMARY : …
FUNCTION : …
| OPERATION [
COMMAND : …
| OPERATION CALL_ARGS
And according to this, “a << rand 3” indeed is a syntax error because
“rand 3” isn’t a FUNCTION but a COMMAND. Since a COMMAND isn’t a
PRIMARY, it can’t stand for an ARG.
Nevetheless, I wonder if I should have known this without checking out
the BNF. Shouldn’t it be “common knowledge” that you can’t do “2 + rand
3” in Ruby?