From: Wejn Date: 2002-10-17T05:47:09+09:00 Subject: Plugin autoregistration (inherited method call) Hi, i'm trying to write some kind of plugin system for my project ... and i really would like to make plugin developer's life easier ... so i decided to carry most of the work in my classes... the problem is i'm unable to get it to work properly :( My original idea was to register 'transports' this way: # --- begin ----------------------------------------------------------------- class Transport @@transports = {} def Transport.add(addr, tgt) @@transports[addr] = tgt end def Transport.registered_transports ar = [] @@transports.each_key { |x| ar << x } ar end end class TCPTransport < Transport Transport.add('tcp', self) end puts "Registered transports: " + Transport.registered_transports.join(', ') # --- end ----------------------------------------------------------------- which works perfectly ... but ... you MUST call "Transport.add('tcp', self)", which is (imho) pretty ugly. I tried to rewrite it this way: # --- begin ----------------------------------------------------------------- class Transport @@transports = {} def Transport.registered_transports ar = [] @@transports.each_key { |x| ar << x } ar end def Transport.inherited(sub) @@transports[sub.new.proto_id] = sub end end class TCPTransport < Transport def proto_id 'tcp' end end puts "Registered transports: " + Transport.registered_transports.join(', ') # --- end ----------------------------------------------------------------- but it just doesn't work ... :( Neither does "@@transports[sub.proto_id] = sub" -- but I can't figure out why. Did I overlooked something (or is there any (better) way)? Thanks in advance, W. PS: I'm still using Ruby 1.6 ... but if there's 1.7 way only ... i'll switch :) -- Wejn (svamberk.net's Linux section, fi.muni.cz student, linuxfan) >>> Bored? Want hours of entertainment? <<< >>> Just set the initdefault to 6! <<<