On Wednesday 20 August 2008, Patrick Li wrote: > Hi, > I'm trying to create a utility method that will automatically create a > class for me, given a classname. But I can't find a way around having to > use eval(). > > This is what I'm doing right now: > > def createClass(className) > eval(<<-EOS) > #{className} = Class.new do > #bla bla bla > end > EOS > end > > but I really don't like eval() and would like to avoid it, if at all > possible. > You're almost there. This works def create_class name, mod = Object cls = Class.new do ... end mod.constant_set name, cls end This allows to put the created class inside any module/class you like. If you don't need it, simply remove it from the argument list and use Object in its place inside the method. I hope this helps Stefano