# How to create object of class name of which is in text strin

**URL:** <https://www.ruby-forum.com/t/how-to-create-object-of-class-name-of-which-is-in-text-strin/77891>\
**Category:** Ruby\
**Created:** [December 1, 2006, 10:31am UTC](https://www.ruby-forum.com/t/how-to-create-object-of-class-name-of-which-is-in-text-strin/77891 "2006-12-01T10:31:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xTRiM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ad7895/32.png) [@xTRiM](https://www.ruby-forum.com/u/xTRiM)\
**Post date:** [December 1, 2006, 10:31am UTC](https://www.ruby-forum.com/t/how-to-create-object-of-class-name-of-which-is-in-text-strin/77891/1 "2006-12-01T10:31:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xTRiM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ad7895/32.png) [@xTRiM](https://www.ruby-forum.com/u/xTRiM)\
**Post date:** [December 1, 2006, 10:35am UTC](https://www.ruby-forum.com/t/how-to-create-object-of-class-name-of-which-is-in-text-strin/77891/2 "2006-12-01T10:35:09Z")

</div>

On 01/12/06, xTRiM [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Bar; end
> 
> 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

---

<div class="post-metadata">

**Author:** ![xTRiM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ad7895/32.png) [@xTRiM](https://www.ruby-forum.com/u/xTRiM)\
**Post date:** [December 1, 2006, 11:25am UTC](https://www.ruby-forum.com/t/how-to-create-object-of-class-name-of-which-is-in-text-strin/77891/3 "2006-12-01T11:25:19Z")

</div>

> create\_something(Kernel.const\_get(“Foo”))
> 
> Farrel

Thanks a lot, Farrel!
