Gavin Kistner
10/26/2006 3:56:00 AM
Phrogz wrote:
> Or do you want the exact same list of methods returned regardless of
> what level you call the method at?
Here's an example showing this end result. (Look ma! A real use for
class variables!)
class Alpha
@@alpha_and_subs = [ self ]
def self.inherited( subklass )
@@alpha_and_subs << subklass
end
def self.all_methods
@@alpha_and_subs.map{ |klass|
klass.instance_methods( false )
}.flatten
end
def alpha1; end
def alpha2; end
end
class Bravo < Alpha
def bravo1; end
end
class Charlie < Bravo
def charlie1; end
end
class Bingo < Alpha
def bingo1; end
end
p Alpha.all_methods
#=> ["alpha1", "alpha2", "bravo1", "charlie1", "bingo1"]
p Bravo.all_methods
#=> ["alpha1", "alpha2", "bravo1", "charlie1", "bingo1"]
p Charlie.all_methods
#=> ["alpha1", "alpha2", "bravo1", "charlie1", "bingo1"]