# Initialize a subclass inside a different namespace

**URL:** <https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-different-namespace/72177>\
**Category:** Ruby\
**Created:** [October 15, 2006, 3:49am UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-different-namespace/72177 "2006-10-15T03:49:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 15, 2006, 3:49am UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-different-namespace/72177/1 "2006-10-15T03:49:20Z")

</div>

have a namespace Foo and a class Bas and a class Bar. I am adding  
classes Bas & Bar are both inside the namespace Foo.

module Foo  
class Bas  
.  
end

class Bar  
.  
end  
end

I have some dynamically added classes that are subclasses of Bar, but I  
want them to be in a namespace under Foo::Bas e.g. Foo::Bas::ThisClass  
& Foo::Bar::ThatClass

class this \< Bar  
.  
end

I put them into the namespace Foo::Bas as follows;

module Foo  
class Bas  
def self.load\_stuff  
module\_eval File.read(file\_name\_where\_this\_class\_is\_defined)  
end  
end  
end

This all works, but I would like to include some code that runs in the  
context of ThisClass but before the contents of  
file\_name\_where\_this\_class\_is\_defined, but the catch is I cannot assume  
the names of the classes being loaded from the file, therefore I am  
looking for a callback that is run in context of ThisClass as soon as  
ThisClass is defined.

Any ideas?

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-different-namespace/72177/2 "2006-10-15T14:16:08Z")

</div>

J2M wrote:

> end  
> I put them into the namespace Foo::Bas as follows;  
> context of ThisClass but before the contents of  
> file\_name\_where\_this\_class\_is\_defined, but the catch is I cannot assume  
> the names of the classes being loaded from the file, therefore I am  
> looking for a callback that is run in context of ThisClass as soon as  
> ThisClass is defined.
> 
> Any ideas?

You might try #inherited.

T.
