Test whether a Ruby class is a subclass of another class Test whether a Ruby class is a subclass of another class ruby ruby

Test whether a Ruby class is a subclass of another class


Just use the < operator

B < A # => trueA < A # => false

or use the <= operator

B <= A # => trueA <= A # => true


Also available:

B.ancestors.include? A

This differs slightly from the (shorter) answer of B < A because B is included in B.ancestors:

B.ancestors#=> [B, A, Object, Kernel, BasicObject]B < B#=> falseB.ancestors.include? B#=> true

Whether or not this is desirable depends on your use case.