# Syntax error I don't understand

**URL:** <https://rubytalk.org/t/syntax-error-i-dont-understand/50901>\
**Category:** ruby-talk\
**Created:** [28 December 2008 10:28 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901 "2008-12-28T10:28:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [28 December 2008 10:28 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/1 "2008-12-28T10:28:33Z")

</div>

(I'm using Ruby 1.8.6.)

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

&nbsp;&nbsp;a = []  
&nbsp;&nbsp;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...

&nbsp;&nbsp;a = []  
&nbsp;&nbsp;a \<\< rand(3)

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Matt\_Harrison](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@Matt\_Harrison](https://rubytalk.org/u/Matt_Harrison)\
**Post date:** [28 December 2008 11:20 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/2 "2008-12-28T11:20:51Z")

</div>

Albert Schlef wrote:

> (I'm using Ruby 1.8.6.)
> 
> I'm trying to add a random number to an array. I do:
> 
> &nbsp;&nbsp;a =   
> &nbsp;&nbsp;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...
> 
> &nbsp;&nbsp;a =   
> &nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Dave\_Bass](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@Dave\_Bass](https://rubytalk.org/u/Dave_Bass)\
**Post date:** [28 December 2008 11:58 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/3 "2008-12-28T11:58:39Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [28 December 2008 12:08 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/4 "2008-12-28T12:08:54Z")

</div>

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](http://web.njit.edu/all_topics/Prog_Lang_Docs/html/ruby/yacc.html)

and I see:

&nbsp;&nbsp;ARG : ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> ...

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [28 December 2008 17:22 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/5 "2008-12-28T17:22:17Z")

</div>

Yeah, I pretty much agree. Here are some good exceptions from myself and other commenters though:

> **[Gray Soft / Not Found](http://graysoftinc.com/articles/do_i_need_these_parentheses)**
>
> Something has gone wrong.

James Edward Gray II

> **···**
>
> On Dec 28, 2008, at 5:58 AM, Dave Bass wrote:
> 
> > Something I learned many years and several languages ago: "When in  
> > doubt, use parentheses."

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [28 December 2008 19:06 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/6 "2008-12-28T19:06:25Z")

</div>

Matt Harrison wrote:

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

Or around the subexpression:

&nbsp;&nbsp;a \<\< (rand 3)

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 December 2008 13:04 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/7 "2008-12-28T13:04:44Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> On 28.12.2008 13:08, Albert Schlef wrote:
> 
> > 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.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [29 December 2008 19:34 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/8 "2008-12-29T19:34:40Z")

</div>

Albert Schlef \<albertschlef@gmail.com\> writes:

> 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 may use parentheses on function invocations more than most, but I  
don't think it's contrary to "Ruby's coding style". Invocations  
without parentheses can be nice for simpler statements and dsl-like  
usage, but I personally prefer parentheses for invocations that aren't  
of the "function [argument, ...]" form - such as the one you began the  
thread with.

On a related note, using parentheses in expressions generally, as Dave  
Bass mentioned, is not a bad practice.

When I was younger, I would pride myself in memorizing the operator  
precedence of various languages, and use the minimum parentheses  
necessary because I thought I was being "cool" or "smart". I now feel  
I was being neither, and it was of little benefit to others when  
reading my code.

> **···**
>
> --  
> Brian Adkins  
> [http://www.lojic.com/](http://www.lojic.com/)

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [28 December 2008 14:57 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/9 "2008-12-28T14:57:40Z")

</div>

Robert Klemme wrote:

> [...]  
> 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] [Ruby gives some warning - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/174345)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dave\_Mail\_Archive](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dave\_Mail\_Archive](https://rubytalk.org/u/Dave_Mail_Archive)\
**Post date:** [28 December 2008 16:30 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/10 "2008-12-28T16:30:24Z")

</div>

compare

&nbsp;&nbsp;&nbsp;a = 99

&nbsp;&nbsp;&nbsp;1 + a 2

and

&nbsp;&nbsp;&nbsp;1 + rand 2

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

In this case, it's your job to disambiguate it by telling Ruby that 2 is a method parameter, and you do that by making it obvious that rand is a method invocation—you put the parentheses.

Regards

Dave Thomas

> **···**
>
> On Dec 28, 2008, at 8:57 AM, Albert Schlef wrote:
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [28 December 2008 17:21 UTC](https://rubytalk.org/t/syntax-error-i-dont-understand/50901/11 "2008-12-28T17:21:45Z")

</div>

Dave Thomas wrote:

> &nbsp;&nbsp;&nbsp;a = 99
> 
> &nbsp;&nbsp;&nbsp;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] [Ruby gives some warning - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/174345)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
