From: Daniel Kempkens Date: 2007-06-16T00:15:07+09:00 Subject: Re: Dynamic Class Initialization Peer Allan schrieb: > Hi all, > I know this can be done, but I can't find out how to do it. I have a > string with the name of the class that I want to initialize, but how do > I do it? > > This is what I tried, > > class = "Product" > product = class.new > > Obviously this does not work or I wouldn't be asking :) So, how do I do > this? > > Peer > I suggest you use this method (out of Rails): def constantize(camel_cased_word) unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end camel_cased_word = "::#{camel_cased_word}" unless $1 Object.module_eval(camel_cased_word, __FILE__, __LINE__) end It's a bit more powerful than the built-in Ruby Version.