From: Albert Schlef Date: 2008-12-28T21:08:54+09:00 Subject: Re: Syntax error I don't understand Dave Bass wrote: > > "When in doubt, use parentheses." This avoids having to learn > complicated rules Matt Harrison 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: http://web.njit.edu/all_topics/Prog_Lang_Docs/html/ruby/yacc.html and I see: ARG : ... | ARG `<<' ARG | ... | ARG `+' ARG | ARG `-' ARG | ARG `*' ARG | ARG `/' ARG | ... | PRIMARY PRIMARY : ... | FUNCTION | ... FUNCTION : ... | OPERATION [`(' [CALL_ARGS] `)'] | ... 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? -- Posted via http://www.ruby-forum.com/.