From: dblack@... Date: 2006-04-27T10:56:23+09:00 Subject: Re: Extending Methods Hi -- On Thu, 27 Apr 2006, Daniel Harple wrote: > On Apr 27, 2006, at 3:26 AM, Geoff Stanley wrote: > >> 1) The child classes wouldn't do >> anything; They'd have to be hooked into the main program somehow... >> troublesome and [...] > > 1) Use Class#inherited > > class Parent > @@children = [] > def self.children > @@children > end > > def self.inherited(child) > @@children << child > end > end > > class Child < Parent > end > > class Child2 < Parent > end > > Parent.children # => [Child, Child2] > # initialize each plugin ... Although.... class GrandChild < Child end Parent.children # => [Child, Child2, GrandChild] You could get around it like this: def self.inherited(child) def child.inherited(grandchild) end @@children << child end :-) or maybe use an instance variable instead of a class variable, since you really want to track the state of a single object: class Parent def self.children @children end def self.inherited(child) @children ||= [] @children << child end end David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" PDF now on sale! http://www.manning.com/black Paper version coming in early May!