# Dynamic class instantiation

**URL:** <https://www.ruby-forum.com/t/dynamic-class-instantiation/52884>\
**Category:** Ruby\
**Created:** [January 19, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/dynamic-class-instantiation/52884 "2006-01-19T22:41:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 19, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/dynamic-class-instantiation/52884/1 "2006-01-19T22:41:32Z")

</div>

Hi all,

I have a situation where I need to dynamically execute an object from a  
String name. I read in pickaxe that as long as a capitalize the class  
name in the string, Ruby will see it as a Constant and thusly allow me  
to use it.

For example:

module Test

class MyClass  
def do\_something  
puts “Something done.”  
end  
end

end

(caller code)

cls = “Test::MyClass”  
require cls  
obj = cls.new  
obj.do\_something

* * *

When I execute the above caller code, I get an error saying

undefined method `new’ for “Test::MyClass”:String (NoMethodError)

So, obviously, Ruby is telling me that there is no new method for the  
String “Test::MyClass”. I’m trying to instantiate the class this way as  
I have specific methods that return object besides String, so I’m  
guessing eval(cls) isn’t appropriate for me. Also, I checked the  
archives on the list with no luck.

Any suggestions?

Thx,

- jason

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 19, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/dynamic-class-instantiation/52884/2 "2006-01-19T22:50:30Z")

</div>

On Fri, 20 Jan 2006, Jason wrote:

> cls = “Test::MyClass”  
> So, obviously, Ruby is telling me that there is no new method for the  
> String “Test::MyClass”. I’m trying to instantiate the class this way as  
> I have specific methods that return object besides String, so I’m  
> guessing eval(cls) isn’t appropriate for me. Also, I checked the  
> archives on the list with no luck.
> 
> Any suggestions?
> 
> Thx,

```
 def klass_stamp(hierachy, *a, &b)
   ancestors = hierachy.split(%r/::/)
   parent = Object
   while((child = ancestors.shift))
     klass = parent.const_get child
     parent = klass
   end
   klass::new(*a, &b)
 end

 class A; class B;end; end

 klass_stamp 'A::B'

```

hth.

-a

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 19, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/dynamic-class-instantiation/52884/3 "2006-01-19T22:59:43Z")

</div>

unknown wrote:

> ```
> def klass_stamp(hierachy, *a, &b)
> ancestors = hierachy.split(%r/::/)
> parent = Object
> while((child = ancestors.shift))
> klass = parent.const_get child
> parent = klass
> end
> klass::new(*a, &b)
> end
> 
> class A; class B;end; end
> 
> klass_stamp 'A::B'
> 
> ```
> 
> hth.
> 
> -a

Actually, I just found this, which is a little simpler, but looks like  
it does the same 🙂

mod = Module  
name.split(/::/).each {|m| mod = mod.const\_get(m) }  
m = mod.new

# Call my method

m.do\_something

Thx,

- jason

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [January 19, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/dynamic-class-instantiation/52884/4 "2006-01-19T23:14:40Z")

</div>

On Jan 19, 2006, at 1:23 PM, Jason wrote:

> > ```
> > end
> > 
> > ```
> 
> Actually, I just found this, which is a little simpler, but looks like  
> it does the same 🙂
> 
> mod = Module  
> name.split(/::/).each {|m| mod = mod.const\_get(m) }  
> m = mod.new

def Object.path2class(path) # matches a C method that is not exported  
into Ruby  
return path.split(‘::’).inject(Object) { |k,n| k.const\_get n }  
end

m = Object.path2class(‘Test::MyClass’).new

> # Call my method
> 
> m.do\_something

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
