Check if MyClass extends OtherClass?

F

forum

Hi all

def OtherClass
end

def MyClass < OtherClass
end

How can I check if MyClass extends OtherClass? When I have an instance I
can call my_class_instance.kind_of?(OtherClass), but what can I call
when only having the class name?

Thanks
Josh
 
V

vjoel

Joshua said:
How can I check if MyClass extends OtherClass? When I have an instance I
can call my_class_instance.kind_of?(OtherClass), but what can I call
when only having the class name?

Use the #< and #<= methods of classes (and modules):

class OtherClass
end

class MyClass < OtherClass
end

classes = [MyClass, OtherClass]
classes.each do |c1|
classes.each do |c2|
puts "#{c1} < #{c2}" if c1 < c2
puts "#{c1} <= #{c2}" if c1 <= c2
end
end

__END__

Output:

MyClass <= MyClass
MyClass < OtherClass
MyClass <= OtherClass
OtherClass <= OtherClass
 
F

forum

Thank you very much! So very semantic and easy that I couldn't have
guessed it as a veteran PHP programmer... ;-)
 

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
474,270
Messages
2,571,348
Members
48,034
Latest member
JaimieBarn

Latest Threads

Top