If I have 4 classes with the following hierarchy:
class MainClass < ActiveRecord::Base
...
end
class SubClassA < MainClass
...
end
class SubClassB < MainClass
...
end
class SubClassC < MainClass
...
end
How can I get a list of the subclasses of MainClass without going through and creating instances of each of the other classes?
In a fresh IRB session I can go in and say
irb(main)> MainClass.descendants
=> []
However if I go through and create instances of each subclass I'll see the following
irb(main)> SubClassA.new
=> #<SubClassA ...>
irb(main)> SubClassB.new
=> #<SubClassB ...>
irb(main)> SubClassC.new
=> #<SubClassC ...>
irb(main)> MainClass.descendants
=> [SubClassA(...), SubClassB(...), SubClassC(...)]
I'm basically looking for a way to programmaticly supply all subclasses so in the future when I want to add SubClassD, SubClassE, etc., I won't have to worry that each one is instantiated in the code before the user can see them.