From: Robert Klemme Date: 2012-10-25T03:44:24+09:00 Subject: Re: Dynamically creating subclasses On Wed, Oct 24, 2012 at 2:52 PM, The Sun wrote: > Thanks to Arlen andXavier, > > I now got it working, using the non eval approach. > > name = "DynamicName1" > class = Class.new(ParentClass) Won't work because "class" is one of the few keywords of Ruby: $ ruby19 -ce 'class = Class.new(String)' -e:1: syntax error, unexpected '=' class = Class.new(String) ^ > Object.const_set name, class > class.new > > Great to be able to do this! Please don't. It's no use to assign the name to a constant because if you do not know names beforehand (i.e. when writing the program) as seems to be the case here you need some form of lookup anyway. You could use eval again, but a more simpler and robust approach (because it avoids name clashes) is this: dyn_classes = {} name = "DynamicName1" dyn_classes[name] = Class.new(ParentClass) Btw, these classes are fairly useless because the only distinguishing factor is the name. You'd also have to generate custom methods for each of those. What is the purpose of these generated classes? Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/