From: Farrel Lifson Date: 2006-12-01T18:33:56+09:00 Subject: Re: How to create object of class name of which is in text string? On 01/12/06, xTRiM wrote: > I know what this is about Abstract Factory pattern. But in example at > rubygarden we passing a class to factory and it creates an object from > it. > But how can i pass a text string to factory which will create an object > from it? > In later example: i wish to pass string "Foo" or "Bar" not Foo or Bar. > > > ------ From rubygarden library of patterns ------ > class Foo; end > class Bar; end > > # Here is the use of the Abstract Factory pattern > def create_something( factory ) > new_object = factory.new > puts "created a new #{new_object.class} with a factory" > end > > # Here we select a factory to use > create_something( Foo ) > create_something( Bar ) > > Running the code above results in the output: > created a Foo with a factory > created a Bar with a factory create_something(Kernel.const_get("Foo")) Farrel