From: Geoff Stanley Date: 2006-04-29T23:58:41+09:00 Subject: Re: Extending Methods Thanks for all the responses! 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 I was planning on using Class#inherited, but not for hooking each plugin in. I was going to use it solely to generate a list of plugins. Each plugin would be a .rb file that starts with a new class that inherits from GenericPlugin, and that class defines the plugin, giving it a name and description etc. Sylvain Joyeux wrote: > You can use Module and super > > class Channel > def initialize > super if defined? super > end > end > > module AChannelPlugin > def initialize > super if defined? super > # Then write custom code. (To open a log file, for example.) > end > end > > channel = Channel.new > channel.extend AChannelPlugin # per-object plugin loading > Channel.include AChannelPlugin # global plugin loading This is a nice solution, except for this: Every method that is inherited (and those that aren't can do it too) has to have that 'super if defined? super' code in it. Perhaps that could be dynamically added to each method as it is created, I'm not sure. I've thought some more about it, and came up with this solution of my own, that does in the end use super and inheritance. class Foo def testage puts "This is the base Foo class" end end class FooPlugin < Foo def testage super puts "Hello from the FooPlugin" end end Foo = FooPlugin class FooPlugin2 < Foo def testage super puts "Hello from FooPlugin2!" end end Foo = FooPlugin2 Foo.new.testage # OUTPUT AS FOLLOWS: #test2.rb:13: warning: already initialized constant Foo #test2.rb:21: warning: already initialized constant Foo #This is the base Foo class #Hello from the FooPlugin #Hello from FooPlugin2! The only problem is those warnings. Is there a different way of overwriting the Foo class with the FooPlugin class so that these warnings are not produced? I really don't want to have to resort to running my program with -W0 (warning level: silent). Thanks once again! -- Posted via http://www.ruby-forum.com/.