# Extend an object using string as module name?

**URL:** <https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437>\
**Category:** Ruby\
**Created:** [December 9, 2005, 3:44am UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437 "2005-12-09T03:44:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [December 9, 2005, 3:44am UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437/1 "2005-12-09T03:44:57Z")

</div>

hello,

is this a proper way to do it:

name = ‘MyModule’  
o = Object.new  
o.extend(Module.const\_get(name))

are there better ways?

thanks  
konstantin

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [December 9, 2005, 4:54am UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437/2 "2005-12-09T04:54:05Z")

</div>

On Fri, 9 Dec 2005, ako… wrote:

> hello,
> 
> is this a proper way to do it:
> 
> name = ‘MyModule’  
> o = Object.new  
> o.extend(Module.const\_get(name))
> 
> are there better ways?

this will fail for “ModuleA::ModuleB”. for that you need something  
like:

```
 #
 # creates a class by class name
 #
   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

this will work for

m = klass\_stamp “A:🅱:C::Module”  
o = Object::new  
o.extend m

it’s in alib.rb btw.

hth.

-a

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [December 9, 2005, 1:49pm UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437/3 "2005-12-09T13:49:20Z")

</div>

On Thu, 08 Dec 2005 18:37:43 -0800, ako… wrote:

> hello,
> 
> is this a proper way to do it:
> 
> name = ‘MyModule’  
> o = Object.new  
> o.extend(Module.const\_get(name))

I don’t know if this is the proper way, but you could just eval the  
name:

name = ‘MyModule’  
o = Object.new  
o.extend( eval name )

Or alternatively:

eval(“o.extend #{name}”)

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [December 9, 2005, 3:43pm UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437/4 "2005-12-09T15:43:57Z")

</div>

require ‘facet/kernel/constant’

name = ‘MyModule’  
o = Object.new  
o.extend(constant(name))

T.

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [December 9, 2005, 3:06pm UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437/5 "2005-12-09T15:06:59Z")

</div>

On Fri, Dec 09, 2005 at 12:50:40PM +0900, removed\_email\_address@domain.invalid wrote:

> ```
> klass = parent.const_get child
> parent = klass
> end
> klass::new(*a, &b)
> 
> ```
> 
> #–}}}  
> end

def klass\_stamp(name, \*a, &b)  
name.split(/::/).inject(Object){|s,x| s.const\_get(x)}.new(\*a,&b)  
end

class A; class B; def foo; “A::B#foo” end end end  
klass\_stamp(“A::B”).foo # =\> “A::B#foo”

> this will work for
> 
> m = klass\_stamp “A:🅱:C::Module”

# ^

# that would try to instantiate the module

```
m = "A::B::C::Module".split(/::/).inject(Object){|s,x| 

```

s.const\_get(x)}

> o = Object::new  
> o.extend m

PS: have you seen  
[eigenclass.org](http://eigenclass.org/hiki.rb?Usable+Ruby+folding+for+Vim) ?  
It might be of interest to you if you often use manual markers for  
methods…

---

<div class="post-metadata">

**Author:** ![akonsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@akonsu](https://www.ruby-forum.com/u/akonsu)\
**Post date:** [December 9, 2005, 4:16pm UTC](https://www.ruby-forum.com/t/extend-an-object-using-string-as-module-name/64437/6 "2005-12-09T16:16:43Z")

</div>

On Fri, 9 Dec 2005, Trans wrote:

> require ‘facet/kernel/constant’
> 
> name = ‘MyModule’  
> o = Object.new  
> o.extend(constant(name))
> 
> T.

we have dueling libraries tom! 😉

guess the fact that we are both writing these function over and over  
means  
they are useful.

cheers.

-a
