# Initialize a subclass inside a namespace

**URL:** <https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-namespace/72174>\
**Category:** Ruby\
**Created:** [October 15, 2006, 3:37am UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-namespace/72174 "2006-10-15T03:37:19Z")\
**Posts on this page:** 4\
**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:37am UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-namespace/72174/1 "2006-10-15T03:37:19Z")

</div>

I 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, 3:51am UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-namespace/72174/2 "2006-10-15T03:51:21Z")

</div>

On 10/14/06, J2M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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  
> …

> 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.

I don’t see a callback when a class (or any constant for that matter)  
is added to a module.

On the other hand, can you do what you want by overriding  
Foo::Bar.inherited ? At least you can tell when a subclass has been  
created.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<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:57am UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-namespace/72174/3 "2006-10-15T03:57:21Z")

</div>

Good idea Dave, I will give that a go.

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, 12:15pm UTC](https://www.ruby-forum.com/t/initialize-a-subclass-inside-a-namespace/72174/4 "2006-10-15T12:15:56Z")

</div>

Whoops replied to the wrong message!

Thanks Rick, that worked perfectly.

James
