How do I know if a class inherits from another? Some methods like is_a?

A simple example:

class A end class B < A end 

Then, how can I judge if class B inherits from class A? Is there a way somehow like is_a? or can be called is_child_of? ?

I can not find him.

+4
source share
3 answers

You can use the < operator:

B < A will be true if B is a subclass of A.

+9
source

In Ruby, does the Object class have a kind_of? method kind_of? that does what you want. Is it also an alias of is_a? :

 module M; end class A include M end class B < A; end class C < B; end b = B.new b.kind_of? A #=> true b.is_of? B #=> true b.kind_of? C #=> false b.kind_of? M #=> true 

In addition, the Class class has a superclass method:

 >> B.superclass => A 

Note that you can find out which methods any object supports:

 B.methods.sort 

Would the result of this command include kind_of? methods kind_of? / is_a? / superclass .

+1
source

You can find all the method definitions for Ruby Objects on the Internet.

The closest useful method would be is_a? or kind_of? however, read the documentation to make sure that this is what you are looking for.

0
source

All Articles