From: Vicente Bosch Campos Date: 2011-03-26T00:33:21+09:00 Subject: Re: Dynamic classes You can give it a dynamic name with eval: ruby-1.9.2-p180 > a = "Monkey" => "Monkey" ruby-1.9.2-p180 > eval("#{a} = Class.new") => Monkey ruby-1.9.2-p180 > b = Monkey.new => # ruby-1.9.2-p180 > It seemed a bit messy so I checked out also Class.new, Module.new and Module.name ( class inherits from module and hence uses the name property) but no luck :-( I wonder whyClass.new and Module.new don't have a parameter to define the name (seems something a person would want to do). Any ideas ? Regards, V. On Mar 25, 2011, at 3:25 PM, PsiPro wrote: > So I am working on some metaprograming and have some questions about > how to get what I need out of dynamic classes. > > Right now I know I can define dynamic classes like so: > a) TestClass = Class.new > b) var_class = Class.new > > These work as expected but I need the functionality provided by case > A. Specifically I need to have the instances respond to .class > appropriately. .class for case B returns Class, while case A responds > TestClass > > The reason I need to do this is that I want to dynamically generate > classes, including their names. How can I accomplish this? > > My desired results: > > variable = Class.new > variable.class.name == 'variable' > > OR, I need to be able to assign a dynamic constant name (I know I > know). > > Any help would be appreciated. >