Printing subclasses

Can Ruby print the subclasses of a given class? Like the reverse of the
example of Class#superclass in “Reflection, ObjectSace and Distributed
Ruby” in pickaxe.

ted wrote:

Can Ruby print the subclasses of a given class? Like the reverse of the
example of Class#superclass in “Reflection, ObjectSace and Distributed
Ruby” in pickaxe.

A heavy handed way is using ObjectSpace:

a = StandardError # just as an example
ObjectSpace.each_object(Class){|i| puts i if i.superclass==a}

Probably exists a neater way that has eluded me so far.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Can Ruby print the subclasses of a given class? Like the reverse of the
example of Class#superclass in “Reflection, ObjectSace and Distributed
Ruby” in pickaxe.

def subclasses(the_class)
result =
ObjectSpace.each_object(Class) do |klass|
result << klass if klass < the_class
end
result
end

p subclasses(Numeric) # → [Bignum, Float, Fixnum, Integer]

Now I am justified in asking something that has bothered me for a while. Why
can’t ObjectSpace return an array of all objects of a given class/module.

Then the whole method above would be written as:

ObjectSpace.objects(Class).select { |klass| klass < the_class }

Makes sense to me.

Gavin

···

From: “ted” ted@php.net

Ted,

If you want a list of all subclasses of a certain class, you could implement
something similar to the following:

class SuperClass
	SUBS = []
	def Superclass.inherited subclass
		Superclass::SUBS << subclass
	end
end

class SubClass1 < SuperClass; end
class SubClass2 < SuperClass; end

Due to the fact SuperClass.inherited is called for each definition of a
subclass (with the subclass given as the argument), the contents of
SuperClass::SUBS would then be:

	[SubClass1, SubClass2]

I’ve seen this used for Plugin classes.

Note this approach does not require that the subclasses be instantiated; IIRC
using ObjectSpace does. Plus I think this is a little less-heavy handed.

Hope that helps,

~ Bruce

···

On Friday 06 December 2002 01:37 am, ted wrote:

Can Ruby print the subclasses of a given class? Like the reverse of the
example of Class#superclass in “Reflection, ObjectSace and Distributed
Ruby” in pickaxe.


Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com

‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams

[snip important context ;]
Note this approach does not require that the subclasses be instantiated; IIRC
using ObjectSpace does. Plus I think this is a little less-heavy handed.

The ObjectSpace solution should only require that the class has been defined.
Or perhaps that what you meant by “instantiated”, since it could certainly be
interpreted that way.

Gavni

···

From: “Bruce Williams” bruce@codedbliss.com

Ok, an honest mistake-- and to clarify on ‘instantiation’; I meant instances
of the actual subclasses being instantiated; not the instantiation of the
subclasses at definition as instances of Class… the whole reason
ObjectSpace sees them in the first place (engages in self-flagelation).

I’d still prefer my approach because it seems a little less ‘heavy’ (because
it seems to take care of itself), but that may be just a matter of taste.

  • Bruce
···

On Friday 06 December 2002 04:54 am, Gavin Sinclair wrote:

The ObjectSpace solution should only require that the class has been
defined. Or perhaps that what you meant by “instantiated”, since it could
certainly be interpreted that way.


Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com

‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams

The ObjectSpace solution should only require that the class has been
defined. Or perhaps that what you meant by “instantiated”, since it could
certainly be interpreted that way.

Ok, an honest mistake-- and to clarify on ‘instantiation’; I meant instances
of the actual subclasses being instantiated; not the instantiation of the
subclasses at definition as instances of Class… the whole reason
ObjectSpace sees them in the first place (engages in self-flagelation).

Hmmm… OK, half marks for applied knowledge :slight_smile:

I’d still prefer my approach because it seems a little less ‘heavy’ (because
it seems to take care of itself), but that may be just a matter of taste.

I prefer it too. It’s a very nice way to gather plugins. Maybe that’s what
the OP had in mind.

  • Bruce

Gavin

···

From: “Bruce Williams” bruce@codedbliss.com

On Friday 06 December 2002 04:54 am, Gavin Sinclair wrote: