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
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
Joshua M. 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
Thank you very much! So very semantic and easy that I couldn’t have
guessed it as a veteran PHP programmer… 
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs