Whitespace matters in instance_eval? I'm confused

H

Harold Hausman

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
 
R

Rick DeNatale

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

Harold Hausman

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

...

Makes good sense, thank you kindly.

-Harold
 
C

Charles Hoffman

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top