From: Joel VanderWerf Date: 2004-06-28T04:51:11+09:00 Subject: Re: accessing a subclass Florian Weber wrote: > hi! > > im having two classes.. fruit and apple. > > i wanna do something like > > class Apple < Fruit > add_something "foobar" > end > > in the fruit class i have a add_something method > which i want to save the "foobar" to a static hash > in the apple class. the hash however should also > not be declared in the apple class. > > is this somehow possible? i guess not, or? just > making sure, since ruby definitely has a lot of suprises =) > > i guess the only other way would be to make a static > hash in the fruit class which is 2d, the first dimension being > the subclass.. You can make use of the fact that class methods are inherited, but class instance variables are not: class Fruit class << self def somethings @somethings ||= {} end def add_something(thing) somethings[thing] = true end end end class Apple < Fruit add_something :spherical end p Fruit.somethings[:spherical] # ==> nil p Apple.somethings[:spherical] # ==> true