Syntax error I don't understand

A

Albert Schlef

(I'm using Ruby 1.8.6.)

I'm trying to add a random number to an array. I do:

a = []
a << rand 3

But ruby replies with "syntax error, unexpected tINTEGER, expecting kDO
or '{' or '('"

Why's that?

When I add parentheses to the function call, as in...

a = []
a << rand(3)

...then I don't get this syntax error. I'd like to understand why this
happens.
 
M

Matt Harrison

Albert said:
(I'm using Ruby 1.8.6.)

I'm trying to add a random number to an array. I do:

a = []
a << rand 3

But ruby replies with "syntax error, unexpected tINTEGER, expecting kDO
or '{' or '('"

Why's that?

When I add parentheses to the function call, as in...

a = []
a << rand(3)

...then I don't get this syntax error. I'd like to understand why this
happens.

As I have learned, the parenthesis are optional for function arguments,
however there are some cases where that confuses the interpreter. It
would be my guess ruby is trying to do "a << rand" and then finds and
extra integer stuck on the end.

Putting the parenthesis around the integer forces ruby to perform the
"<<" on the whole function.

HTH

Matt
 
D

Dave Bass

Something I learned many years and several languages ago: "When in
doubt, use parentheses." This avoids having to learn complicated rules
governing operator precedence etc, rules which can vary from one
language to another.

As a result my Ruby code probably has more () pairs than strictly
necessary. But I believe in letting the computer do the work rather than
busting my brain to reduce the source code by a few bytes.

As a compromise I sometimes strip out () pairs until the code fails the
unit tests, and then replace the last pair. ;-)

Dave
 
A

Albert Schlef

Dave said:
"When in doubt, use parentheses." This avoids having to learn
complicated rules

Matt said:
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?
 
R

Robert Klemme

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.

My approach is a different one - maybe you can use it, too: if I
encounter a syntax error, I try to see what's wrong. Then I add
parentheses and if the syntax error goes away I leave it at that. I
don't waste too much brain cycles on syntax - IMHO that's wasted energy
unless you are designing a DSL. :)

Cheers

robert
 
A

Albert Schlef

Robert said:
[...]
I don't waste too much brain cycles on syntax
- IMHO that's wasted energy

I'm not a strict fellow. I don't mind putting extra parentheses.

I don't mind the "2 + rand 3" issue in itself. The problem is that I've
just encountered yet another similar trivial issue[1], and it gives me a
bad impression of the language. It's as if this compiler was sloppily
written. The reason I want to find the answers is to restore my faith in
this very beautiful language.

[1] http://www.ruby-forum.com/topic/174345
 
D

Dave Thomas

I don't mind the "2 + rand 3" issue in itself. The problem is that =20
I've
just encountered yet another similar trivial issue[1], and it gives =20=
me a
bad impression of the language. It's as if this compiler was sloppily
written. The reason I want to find the answers is to restore my =20
faith in
this very beautiful language.

compare

a =3D 99

1 + a 2

and

1 + rand 2

Clearly the first one is incorrect: you'd expect to see the message =20
"unexpected integer" (the 2, in this case). But syntactically, the =20
second statement is identical to the first.

In this case, it's your job to disambiguate it by telling Ruby that 2 =20=

is a method parameter, and you do that by making it obvious that rand =20=

is a method invocation=97you put the parentheses.


Regards


Dave Thomas=
 
A

Albert Schlef

Dave said:
a = 99

1 + a 2 [...]
it's your job to disambiguate it by telling Ruby that 2
is a method parameter, and you do that by [...] parentheses.

Aha!

Dave, that makes much sense! I'm happy with that answer. Thanks.

.........

Could you check out the other thread I opened[1] ? I'd be a bit
disappointed if this "to disambiguate variable from method" answer
applies there as well because we have to set a limit somewhere.

[1] http://www.ruby-forum.com/topic/174345
 
B

Brian Candler

Matt Harrison wrote:
g> Putting the parenthesis around the integer forces ruby to perform the
"<<" on the whole function.

Or around the subexpression:

a << (rand 3)

You could say that rand 3 is then treated as a separate phrase of
'poetry mode'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top