[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Check if MyClass extends OtherClass?

Joshua Muheim

11/4/2007 11:09:00 PM

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
--
Posted via http://www.ruby-....

2 Answers

Joel VanderWerf

11/4/2007 11:19:00 PM

0

Joshua Muheim wrote:
> 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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joshua Muheim

11/4/2007 11:59:00 PM

0

Thank you very much! So very semantic and easy that I couldn't have
guessed it as a veteran PHP programmer... ;-)
--
Posted via http://www.ruby-....