From: Mauricio Fernandez Date: 2006-05-13T18:03:21+09:00 Subject: Re: Making sense of the various Ruby "eval"s On Sat, May 13, 2006 at 05:28:19PM +0900, Eli Bendersky wrote: > ts wrote: > >>>>>> "E" == Eli Bendersky writes: > > > > E> What have I missed ? > > > > The evilness of eval. > > > > Guy Decoux > > Guy, > > I would really appreciate a serious answer. The Ruby documentation is > bad, there's no need to tell you that, and I'm trying to understand > things in depth, hence my questions. > > I understand that eval can be mis-used, like many other advanced > features. > > However, I much prefer a serious to-the-point answer. You have cleared > out a lot for me, and I thank you for that. What is left is my last > question: > > Is block eval the same as single-quoted-string eval ? $ cat string.rb def foo instance_eval %{ 387 8sbf fy"(/�%" /�/% "� �� sdug sdf } end puts "I can't foresee any problem." # much later, in code far, far away #foo $ ruby string.rb I can't foresee any problem. $ cat block.rb def foo instance_eval do 387 8sbf fy"(/�%" /�/% "� �� sdug sdf end end puts "I can't foresee any problem." # much later, in code far, far away #foo $ ruby block.rb block.rb:4: syntax error, unexpected tINTEGER, expecting kEND 387 8sbf fy"(/�%" /�/% "� �� sdug sdf ^ block.rb:4: Invalid char `\302' in expression block.rb:4: Invalid char `\267' in expression block.rb:4: unterminated string meets end of file block.rb:4: syntax error, unexpected tSTRING_END, expecting tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR Another difference: $ cat eval.rb def foo_string(str) eval str instance_eval "var" end def foo_block(str) eval str instance_eval { var } end puts foo_string("var = 'some value'") puts foo_block("var = 'some value'") $ ruby eval.rb some value eval.rb:8:in `foo_block': undefined local variable or method `var' for main:Object (NameError) from eval.rb:8:in `foo_block' from eval.rb:12 -- Mauricio Fernandez - http://eigenclass.org - singular Ruby