Is there a way to know what are the subclasses of a given class ?

S

Sandro Paganotti

[Note: parts of this message were removed to make it a legal post.]

Hello

Do you know if there's a trick to retrieve a list of all the subclasses of a
given class ?

class A
class B
end
class C
end
end

like A.subclasses --> [B,C] ?

Thanks
Sandro
 
S

Sandro Paganotti

[Note: parts of this message were removed to make it a legal post.]

[SOLVED]

I just find out that you can call:

A.constants --> ["B","C"]

Thank you

Sandro

On Wed, Nov 19, 2008 at 10:10 AM, Sandro Paganotti <
Hello

Do you know if there's a trick to retrieve a list of all the subclasses of
a given class ?

class A
class B
end
class C
end
end

like A.subclasses --> [B,C] ?

Thanks
Sandro
 
P

Peter Szinek

[Note: parts of this message were removed to make it a legal post.]

In your above example, B is not subclass of A (B is not even defined,
just A::B).

Also, how about

class A
class B
end
class C
end
D = E = F = 5
end
=> ["C", "B", "D", "F", "E"]

What are you trying to do exactly? Maybe modules would be better?

Cheers,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org

[SOLVED]

I just find out that you can call:

A.constants --> ["B","C"]

Thank you

Sandro

On Wed, Nov 19, 2008 at 10:10 AM, Sandro Paganotti <
Hello

Do you know if there's a trick to retrieve a list of all the
subclasses of
a given class ?

class A
class B
end
class C
end
end

like A.subclasses --> [B,C] ?

Thanks
Sandro
 
B

Brian Candler

Sandro said:
Do you know if there's a trick to retrieve a list of all the subclasses
of a
given class ?

Only this that I know of:

class A; end
class B<A; end

require 'enumerator'
p ObjectSpace.to_enum:)each_object, Class).
select { |c| c.superclass == A }
 
S

Sandro Paganotti

[Note: parts of this message were removed to make it a legal post.]

Thanks Peter :D

That exactly what I was lloking for :)

In your above example, B is not subclass of A (B is not even defined, just
A::B).

Also, how about

class A
class B
end
class C
end
D = E = F = 5
end
=> ["C", "B", "D", "F", "E"]

What are you trying to do exactly? Maybe modules would be better?

Cheers,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org


On 2008.11.19., at 10:10, Sandro Paganotti wrote:

[SOLVED]
I just find out that you can call:

A.constants --> ["B","C"]

Thank you

Sandro

Hello
Do you know if there's a trick to retrieve a list of all the subclasses
of
a given class ?

class A
class B
end
class C
end
end

like A.subclasses --> [B,C] ?

Thanks
Sandro
 
J

Jesús Gabriel y Galán

[SOLVED]

I just find out that you can call:

A.constants --> ["B","C"]

Thank you

Sandro

On Wed, Nov 19, 2008 at 10:10 AM, Sandro Paganotti <
Hello

Do you know if there's a trick to retrieve a list of all the subclasses
of
a given class ?

class A
class B
end
class C
end
end

like A.subclasses --> [B,C] ?

As has been mentioned, in your example B and C are not subclasses of A.
If you want to list all subclasses of a class you can do this:

irb(main):001:0> class A
irb(main):002:1> class << self
irb(main):003:2> attr_accessor :subclasses
irb(main):004:2> end
irb(main):005:1> def self.inherited child
irb(main):006:2> (A.subclasses ||= []) << child
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> A.subclasses
=> nil
irb(main):010:0> class B < A; end;
irb(main):011:0* A.subclasses
=>
irb(main):012:0> class C < B; end;
irb(main):013:0* A.subclasses
=> [B, C]
irb(main):014:0> class D < A; end;
irb(main):015:0* A.subclasses
=> [B, C, D]

Hope this helps,

Jesus.
 
J

Jesús Gabriel y Galán

Little different style maybe:

class A
@subclasses =3D []
def self.inherited(into) @subclasses << into end
def self.subclasses() @subclasses end
end

Yep, much cleaner :).
I made a mess while I was typing in irb about closing the << self early,
and forgetting to initialize the array, so that was the result. I
probably wanted to type this:

irb(main):001:0> class A
irb(main):002:1> class << self
irb(main):003:2> attr_reader :subclasses
irb(main):004:2> def inherited(child)
irb(main):005:3> subclasses << child
irb(main):006:3> end
irb(main):007:2> end
irb(main):008:1> @subclasses =3D []
irb(main):009:1> end

but anyway your version is cleaner...

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top