----- Original Message -----
From: <mjbjr / beaudesign.com>
To: ruby-talk ML <ruby-talk / ruby-lang.org>
Sent: Friday, October 19, 2001 10:44 PM
Subject: [ruby-talk:22808] brace block oddity(?), curiousity


> Just curious...
>
> This works:
>
> # process each line
> # works
> addressbook.each_line {|line|
>     print line
> }
>
>
> This doesn't work:
>
> # doesn't work
> addressbook.each_line
> {|line|
>     print line
> }
>
>
> Is there a reason that the interpreter parser(?) doesn't handle the second
> example the same way it handles the first?
>

Here's basically what is happening.

When you put the block on the next line, you're
effectively omitting the block from the method
each_line above. So you get a syntax error when
the next statement appears to be a bare block
unattached to anything.

If you leave the block off entirely, just saying
    addressbook.each_line
this will be clearer. Now it complains about yield
being called without a block.

Basically a method call is a single line unless the
parser perceives that the statement is continued
onto another line. (For example, by an unclosed
brace, a comma, an explicit line continuation...)
The block can start on this line and finish later
(or all be on one line).

Some people (myself included) reserve braces for
single-line blocks and do-end for multiple lines. The
nesting looks better when they are not mixed:
        end
      }
    end
versus:
        end
      end
    end

If you desperately want to start the block on the next
line, you could always use the (ugly) backslash trick:

    addressbook.each_line  \
    {
       #...
    }

Hope this helps.

Cheers,
Hal