[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Initialize a subclass inside a namespace

J2M

10/15/2006 1:31:00 AM

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

3 Answers

Rick DeNatale

10/15/2006 1:49:00 AM

0

On 10/14/06, J2M <james2mccarthy@gmail.com> wrote:
> 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
...

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

J2M

10/15/2006 1:56:00 AM

0

Good idea Dave, I will give that a go.

Thanks,
James

On Oct 15, 2:49 am, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
> On 10/14/06, J2M <james2mccar...@gmail.com> wrote:
>
> > 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...
>
>
>
> > 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 Rubyhttp://talklikeaduck.denh...


J2M

10/15/2006 10:14:00 AM

0

Whoops replied to the wrong message!

Thanks Rick, that worked perfectly.

James

On Oct 15, 2:56 am, "J2M" <james2mccar...@gmail.com> wrote:
> Good idea Dave, I will give that a go.
>
> Thanks,
> James
>
> On Oct 15, 2:49 am, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
>
> > On 10/14/06, J2M <james2mccar...@gmail.com> wrote:
>
> > > 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...
>
> > > 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 Rubyhttp://talklikeaduck.denh...