# Whitespace matters in instance\_eval? I'm confused

**URL:** <https://www.ruby-forum.com/t/whitespace-matters-in-instance-eval-im-confused/101564>\
**Category:** Ruby\
**Created:** [August 6, 2006, 6:58am UTC](https://www.ruby-forum.com/t/whitespace-matters-in-instance-eval-im-confused/101564 "2006-08-06T06:58:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [August 6, 2006, 6:58am UTC](https://www.ruby-forum.com/t/whitespace-matters-in-instance-eval-im-confused/101564/1 "2006-08-06T06:58:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [August 6, 2006, 7:10am UTC](https://www.ruby-forum.com/t/whitespace-matters-in-instance-eval-im-confused/101564/2 "2006-08-06T07:10:25Z")

</div>

On 8/6/06, Harold H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

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\>

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [August 6, 2006, 7:22am UTC](https://www.ruby-forum.com/t/whitespace-matters-in-instance-eval-im-confused/101564/3 "2006-08-06T07:22:26Z")

</div>

On 8/6/06, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > #is there a rule that says that the opening curly brace has to be on  
> > looks like (and is a complete expression, and a complete statement)
> 
> 1. 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

---

<div class="post-metadata">

**Author:** ![Harold\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6de8d8/32.png) [@Harold\_H](https://www.ruby-forum.com/u/Harold_H)\
**Post date:** [August 6, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/whitespace-matters-in-instance-eval-im-confused/101564/4 "2006-08-06T17:53:04Z")

</div>

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

> boom  
> the same line?
> 
> Any help appreciated,  
> -Harold

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–
