Forum: Ruby Block Syntax Error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8f96c3f92bb41dc5c1bc345110923d46?d=identicon&s=25 zhimin.wen (Guest)
on 2005-11-18 07:29
(Received via mailing list)
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?
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 hal9000 (Guest)
on 2005-11-18 07:38
(Received via mailing list)
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
A36e2ad7a1dd10b9709f797344ea5068?d=identicon&s=25 christophe.grandsire (Guest)
on 2005-11-18 08:36
(Received via mailing list)
Selon newbie <zhimin.wen@gmail.com>:

>
> 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.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 matz (Guest)
on 2005-11-18 08:42
(Received via mailing list)
Hi,

In message "Re: Block Syntax Error"
    on Fri, 18 Nov 2005 15:27:18 +0900, "newbie" <zhimin.wen@gmail.com>
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-18 14:36
(Received via mailing list)
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
1d6252c8fa730e6b9989db64df35103a?d=identicon&s=25 J. Merrill (j-merrill)
on 2005-11-18 17:42
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-18 17:52
(Received via mailing list)
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
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 hal9000 (Guest)
on 2005-11-19 01:10
(Received via mailing list)
Yukihiro Matsumoto wrote:
>
>   func("a str"{puts "blk"})
>
> which is not a valid syntax.

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


Hal
8f96c3f92bb41dc5c1bc345110923d46?d=identicon&s=25 zhimin.wen (Guest)
on 2005-11-19 03:59
(Received via mailing list)
Hi,
Thanks all for the clarification and guides!
This topic is locked and can not be replied to.