# Block Syntax Error

**URL:** <https://www.ruby-forum.com/t/block-syntax-error/48254>\
**Category:** Ruby\
**Created:** [November 18, 2005, 7:29am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254 "2005-11-18T07:29:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 7:29am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/1 "2005-11-18T07:29:54Z")

</div>

Hi,  
Please see the following code  
===\>8=======================  
def func \*args,&blk  
puts \*args  
instance\_eval &blk  
end

func(“a str”) {puts “blk”} #OK  
func “a str” do puts “blk” end #OK

func “a str” {puts “blk”} #Sytax Error!?  
===\>8=======================

The last call give a syntax error, which part confuse ruby?

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 7:38am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/2 "2005-11-18T07:38:56Z")

</div>

newbie wrote:

> func “a str” {puts “blk”} #Sytax Error!?  
> ===\>8=======================
> 
> The last call give a syntax error, which part confuse ruby?

In my personal opinion, this is one of those things that  
“should” work. But the Ruby parser is very complex. (Just  
ask anyone who has tried to write another one.)

I am not sure exactly what is happening here, but I would  
tend to think it is a side effect of the way the yacc  
grammar is written.

Hal

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 8:36am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/3 "2005-11-18T08:36:12Z")

</div>

Selon newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> func “a str” {puts “blk”} #Sytax Error!?  
> ===\>8=======================
> 
> The last call give a syntax error, which part confuse ruby?

I wonder if it has to do with precedence rules. The different block  
syntaxes  
have different precendence rules, with {} binding more strongly than  
do… end.  
{} only binds to the last expression, while do… end binds to the whole  
expression. In other words, when you write:

func “a str” do puts “blk” end

Ruby sees:

func(“a str”) do puts “blk” end

(you may also think of it as: ‘(func “a str”) do puts “blk” end’, which  
emphasises the precedence, although I don’t know if it is correct Ruby  
syntax)

However, when you write:

func “a str” {puts “blk”}

Ruby sees:

func(“a str” {puts “blk”})

The binding is tighter, and results in a syntax error since a string  
can’t take  
a block.

## I don’t know why the two block syntaxes have different precedence, but I take it there’s a reason (just like there’s a reason for the existence of “and” and “”&&" with identical meanings but different precedences).

Christophe G…

[http://rainbow.conlang.free.fr](http://rainbow.conlang.free.fr)

It takes a straight mind to create a twisted conlang.

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 8:42am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/4 "2005-11-18T08:42:14Z")

</div>

Hi,

In message “Re: Block Syntax Error”  
on Fri, 18 Nov 2005 15:27:18 +0900, “newbie” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|func “a str” {puts “blk”} #Sytax Error!?

Since

func foo {puts “blk”}

is parsed as

func(foo{puts “blk”})

then I think

func “a str” {puts “blk”}

should be parsed as

func(“a str”{puts “blk”})

which is not a valid syntax.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 2:36pm UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/5 "2005-11-18T14:36:02Z")

</div>

Hi –

On Fri, 18 Nov 2005, newbie wrote:

> func “a str” {puts “blk”} #Sytax Error!?  
> ===\>8=======================
> 
> The last call give a syntax error, which part confuse ruby?

See other answers. I just want to put in a plug (since you’re still a  
nuby 🙂 for using parentheses, especially in method definitions:

def func(\*args, &blk)

I know it looks sort of cool without them, but I find (and I’m pretty  
sure I’m far from the only one) that I have more trouble parsing it  
visually, especially when you get things like:

def a b, c = 100

which is legal but, to my eye, too thin on visual cues.

David

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 5:52pm UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/6 "2005-11-18T17:52:47Z")

</div>

Hi –

On Sat, 19 Nov 2005, J. Merrill wrote:

> > func “a str” {puts “blk”} #Sytax Error!?  
> > the block to func you’d write  
> > func foo, {puts “blk”}

There’s never a comma before a code block. You’d want to do:

func(foo) { puts “blk” }

to pass both the argument and the block to func.

David

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 18, 2005, 5:42pm UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/7 "2005-11-18T17:42:59Z")

</div>

zhimin.wen wrote:

> ===\>8=======================  
> def func \*args,&blk  
> puts \*args  
> instance\_eval &blk  
> end
> 
> func(“a str”) {puts “blk”} #OK  
> func “a str” do puts “blk” end #OK
> 
> func “a str” {puts “blk”} #Sytax Error!?  
> ===\>8=======================
> 
> The last call give a syntax error, which part confuse ruby?

My opinion is that you should _want_ to put a comma before the {puts  
“blk”} to make it clear that you are passing a block parameter to func,  
rather than to “a str”. Suppose the line were  
func foo {puts “blk”}  
where foo is a method – in that case, the block is being passed to foo  
(and the result of foo is being passed to func), and if you’re passing  
the block to func you’d write  
func foo, {puts “blk”}

There are disadvantages to many attempts to reduce keystrokes, IMO.

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 19, 2005, 3:59am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/8 "2005-11-19T03:59:37Z")

</div>

Hi,  
Thanks all for the clarification and guides!

---

<div class="post-metadata">

**Author:** ![zhimin.wen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c2a13f/32.png) [@zhimin.wen](https://www.ruby-forum.com/u/zhimin.wen)\
**Post date:** [November 19, 2005, 1:10am UTC](https://www.ruby-forum.com/t/block-syntax-error/48254/9 "2005-11-19T01:10:51Z")

</div>

Yukihiro M. wrote:

> func(“a str”{puts “blk”})
> 
> which is not a valid syntax.

OK, this makes sense. I should withdraw my previous comments then.

Hal
