From: Robert Klemme Date: 2003-07-30T11:14:01+09:00 Subject: Re: Trouble with inherited "Steve Tuckner" schrieb im Newsbeitrag news:0c3a01c352d9$16fc9a20$6904a8c0@multitech.prv... > In my case, I am interested in getting a string that would be specific to > that class (and not the class name). I am using this name as a hash key from > the name to the class. > > Now I can simply save the class in a list and then when the hash would be > used later, I can simply generate the hash at that time for each request. > This seems not as elegant but probably is not much of a real speed penalty. > > Is there a better more elegant way of doing this? Why don't you just do this? class A @@subhash = {} def self.get(key) @@subhash[key] end end class B < A @@subhash["my key"]=self end A.get("my key") Subclasses have to obey a certain convention anyway (i.e. providing the key) so you might as well do it this way. Otherwise you could do class A class << self def inherited(subclass) (@subqueue||=[]) << subclass end def lookup(key) while ( @subqueue && cl = @subqueue.shift ) (@subhash||={})[ cl.my_name ] = cl end @subhash && @subhash[key] end end end class B < A class << self def my_name "the B class" end end end A.lookup "the B class" But I'd regard this a bit complicated. robert