From: Christophe Grandsire Date: 2005-11-18T16:35:40+09:00 Subject: Re: Block Syntax Error Selon newbie : > 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? > 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 Grandsire. http://rainbow.conlang.free.fr It takes a straight mind to create a twisted conlang.