From: Wilson Bilkovich Date: 2006-05-04T00:30:54+09:00 Subject: Re: how to create Class object with name determined at runti On 5/3/06, Francisco Ortiz wrote: > On 5/1/06, Gregory Seidman wrote: > > > > Avoid using eval when possible. > > > > Hi Greg, would you mind telling me why? is this more expensive? insecure? > I�m another newcomer to Ruby and this kind of tips are gold to me. > Mostly speed, but many people (myself included) find it easier to read. const_get is about 10 times faster than eval: ruby eval_bench.rb user system total real eval: 1.140000 0.000000 1.140000 ( 1.141000) const_get: 0.157000 0.000000 0.157000 ( 0.156000) #eval_bench.rb require 'benchmark' class Foo;end some_class = 'Foo' Benchmark.bm(10) do |b| b.report("eval:") { 100000.times {eval("#{some_class}.new")} } b.report("const_get:") { 100000.times{Object.const_get(some_class).new} } end