# External Module.nesting

**URL:** <https://www.ruby-forum.com/t/external-module-nesting/126526>\
**Category:** Ruby\
**Created:** [January 19, 2008, 12:54am UTC](https://www.ruby-forum.com/t/external-module-nesting/126526 "2008-01-19T00:54:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [January 19, 2008, 12:54am UTC](https://www.ruby-forum.com/t/external-module-nesting/126526/1 "2008-01-19T00:54:52Z")

</div>

Is there an obvious method for a module like  
A:🅱:C  
that would result in A::B?

Module.nesting came to mind, but no I didn’t see an obvious way to make  
it work.

Daniel Brumbaugh K.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [January 19, 2008, 1:31am UTC](https://www.ruby-forum.com/t/external-module-nesting/126526/2 "2008-01-19T01:31:07Z")

</div>

On Jan 19, 2008, at 1:07 AM, Xavier N. wrote:

> On Jan 19, 2008, at 12:29 AM, Daniel Brumbaugh K. wrote:
> 
> > Is there an obvious method for a module like  
> > A:🅱:C  
> > that would result in A::B?
> 
> What do you mean?

Oh, perhaps you just want something like Module#parent provided by  
Active Support. There the parent is computed by hand from the module  
name:

```
def parent
  parent_name = name.split('::')[0..-2] * '::'
  parent_name.empty? ? Object : parent_name.constantize
end

```

where String#constantize is

```
def constantize(camel_cased_word)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError, "#{camel_cased_word.inspect} is not a valid

```

constant name!"  
end

```
  Object.module_eval("::#{$1}", __FILE__ , __LINE__ )
end

```

– fxn

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [January 19, 2008, 1:08am UTC](https://www.ruby-forum.com/t/external-module-nesting/126526/3 "2008-01-19T01:08:19Z")

</div>

On Jan 19, 2008, at 12:29 AM, Daniel Brumbaugh K. wrote:

> Is there an obvious method for a module like  
> A:🅱:C  
> that would result in A::B?

What do you mean?

You know, modules are objects, constants are stored in modules. Given  
those rules you can put a module almost anywhere. AFAICT what you  
cannot change via API is its name, which is set in stone the first  
time a module is assigned to a constant. That happens in regular  
module definitions, or explicitly like this:

```
 $ irb
 irb(main):001:0> m = Module.new
 => #<Module:0x20eb6c>
 irb(main):002:0> m.name
 => ""
 irb(main):003:0> M = m
 => M
 irb(main):004:0> m.name
 => "M"
 irb(main):005:0> N = M
 => M
 irb(main):006:0> N.name
 => "M"

```

– fxn
