From: Ross Bamford Date: 2006-05-14T02:24:11+09:00 Subject: Re: Making sense of the various Ruby "eval"s On Sat, 2006-05-13 at 14:52 +0900, Eli Bendersky wrote: > ts wrote: > >>>>>> "E" == Eli Bendersky writes: > > > > E> I'm struggling to fully understand the difference between the block > > and > > E> the string versions ? > > > > the string version is evil :-) > > > [...] > The way I see it, the block version is simply a convenience, making the > code cleaner. It is similar to wrapping a string into a single > (non-interpolating) quote: As well as the difference in when the code is parsed mentioned in another post, it's probably worth noting that string eval is slower than block eval: $ cat eval_bench2.rb require 'benchmark' o = Object.new Benchmark.bm(10) do |b| b.report("eval str:") { 10000.times {o.instance_eval '10.times { |i| l = 300 * i }' }} b.report("eval blk:") { 10000.times {o.instance_eval { 10.times { |i| l = 300 * i }} }} end $ ruby -v eval_bench2.rb ruby 1.8.4 (2005-12-24) [i686-linux] user system total real eval str: 0.340000 0.000000 0.340000 ( 0.416134) eval blk: 0.160000 0.000000 0.160000 ( 0.203212) In yarv, the difference is even more marked, and from what I can gather it's likely to stay that way: $ ruby-yarv -v eval_bench2.rb ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08) [i686-linux] YARVCore 0.4.0 Rev: 497 (2006-05-07) [opts: ] user system total real eval str: 0.820000 0.020000 0.840000 ( 0.836336) eval blk: 0.060000 0.000000 0.060000 ( 0.105170) -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk