Whitespace matters in instance_eval? I'm confused

irb(main):001:0> foo = Object.new
=> #<Object:0x2b9bb88>
irb(main):002:0> foo.instance_eval "3.times{ puts 'boom' }"
boom
=> 3
irb(main):003:0> foo.instance_eval "3.times{ puts 'boom' \n}"
boom
=> 3
irb(main):004:0> foo.instance_eval "3.times\n{ puts 'boom' \n}"
SyntaxError: (eval):3:in `irb_binding': compile error
(eval):2: syntax error
{ puts 'boom'
        ^
(eval):3: syntax error
        from (irb):4

#is there a rule that says that the opening curly brace has to be on
the same line?

Any help appreciated,
-Harold

I think that it pretty much falls out of the way Ruby handles lines in
source. As the pickaxe put's it Ruby is 'line-oriented'

1) An expression or statement which looks like complete by the end of
the line IS considered complete

2) Blocks are optional in method calls, so
     3.times
    looks like (and is a complete expression, and a complete statement)

3) a bare block is not a valid statement

It's got nothing to do with instance_eval

irb(main):001:0> 3.times
LocalJumpError: no block given
        from (irb):1:in `times'
        from (irb):1
irb(main):002:0> { puts "boom" }
SyntaxError: compile error
(irb):2: parse error, unexpected tSTRING_BEG, expecting kDO or '{' or '('
{ puts "boom" }
        ^
(irb):2: parse error, unexpected '}', expecting $
        from (irb):2
irb(main):003:0>

···

On 8/6/06, Harold Hausman <hhausman@gmail.com> wrote:

irb(main):004:0> foo.instance_eval "3.times\n{ puts 'boom' \n}"
SyntaxError: (eval):3:in `irb_binding': compile error
(eval):2: syntax error
{ puts 'boom'
        ^
(eval):3: syntax error
        from (irb):4

#is there a rule that says that the opening curly brace has to be on
the same line?

--
Rick DeNatale

I think maybe there is. In any case, the common/recommended practice
seems to be to use do..end for multi-line blocks rather than curly
braces. And every piece of Ruby code I've seen to date puts the opening
curly brace or do on the same line as the method call the block is being
passed to.

I'm guessing that

foo.instance_eval "3.times do \n puts 'boom' \n end

would work, but

foo.instance_eval "3.times \n do \n puts 'boom' \n end

would not.

--ch--

···

On Sun, 2006-08-06 at 13:58 +0900, Harold Hausman wrote:

irb(main):001:0> foo = Object.new
=> #<Object:0x2b9bb88>
irb(main):002:0> foo.instance_eval "3.times{ puts 'boom' }"
boom
boom
boom
=> 3
irb(main):003:0> foo.instance_eval "3.times{ puts 'boom' \n}"
boom
boom
boom
=> 3
irb(main):004:0> foo.instance_eval "3.times\n{ puts 'boom' \n}"
SyntaxError: (eval):3:in `irb_binding': compile error
(eval):2: syntax error
{ puts 'boom'
        ^
(eval):3: syntax error
        from (irb):4

#is there a rule that says that the opening curly brace has to be on
the same line?

Any help appreciated,
-Harold

> irb(main):004:0> foo.instance_eval "3.times\n{ puts 'boom' \n}"
> SyntaxError: (eval):3:in `irb_binding': compile error
> (eval):2: syntax error
> { puts 'boom'
> ^
> (eval):3: syntax error
> from (irb):4
>
> #is there a rule that says that the opening curly brace has to be on
> the same line?

I think that it pretty much falls out of the way Ruby handles lines in
source. As the pickaxe put's it Ruby is 'line-oriented'

1) An expression or statement which looks like complete by the end of
the line IS considered complete

2) Blocks are optional in method calls, so
     3.times
    looks like (and is a complete expression, and a complete statement)

3) a bare block is not a valid statement

It's got nothing to do with instance_eval

   ...

--
Rick DeNatale

Makes good sense, thank you kindly.

-Harold

···

On 8/6/06, Rick DeNatale <rick.denatale@gmail.com> wrote:

On 8/6/06, Harold Hausman <hhausman@gmail.com> wrote: