Determining a class's ancestor easily without instances

W

Wes Gamble

Given two classes A and B, I know that I can loop through the
superclasses of A to determine it it's a descendant of B.

Is there a more expedient way to do this - something like "is_a?" but at
the class level (since is_a? is for objects)?

Thanks,
Wes
 
J

Joel VanderWerf

Wes said:
Given two classes A and B, I know that I can loop through the
superclasses of A to determine it it's a descendant of B.

Is there a more expedient way to do this - something like "is_a?" but at
the class level (since is_a? is for objects)?

if A < B
###
end

if A <= B
###
end

[A, B].sort
 
W

Wes Gamble

Joel said:
Wes said:
Given two classes A and B, I know that I can loop through the
superclasses of A to determine it it's a descendant of B.

Is there a more expedient way to do this - something like "is_a?" but at
the class level (since is_a? is for objects)?

if A < B
###
end

if A <= B
###
end

[A, B].sort

So is "<" the "superclass" assignment operator? Is it a method?

Seems like it operates differently in two contexts. It assigns in a
class declaration, but evaluates in a logical expression?

Am I understanding correctly?

Wes
 
P

Pit Capitain

Wes said:
I looked all over the Pickaxe reference to find it but I couldn't it.

I guess the "basic" operators are not documented, even when they're
overridden in clever ways?

Wes, you have to know that "Class" is a subclass of "Module", and so it
inherits all methods from "Module", among them the method "<". See


http://phrogz.net/ProgrammingRuby/ref_c_module.html#Module._lt_cm_lt_eq_cm_lt_cm_lt_eq

The text says:

One module is considered greater than another if it is included in
*(or is a parent class of)* the other module.

Regards,
Pit
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top