219

I would like to test whether a class inherits from another class, but there doesn't seem to exist a method for that.

class A
end

class B < A
end

B.is_a? A 
=> false

B.superclass == A
=> true

(N.B. the is_a? test above does not make any sense, as the first comment correctly points out B.new.is_a?(A) would make sense, but is not generally applicable, as not every class has a #initialize method that accepts 0 arguments)

A trivial implementation of what I want would be:

class Class
  def is_subclass_of?(clazz)
    return true if superclass == clazz
    return false if self == Object
    superclass.is_subclass_of?(clazz)
  end
end

but I would expect this to exist already.

4
  • 3
    A.class #=> Class. This is why B.is_a? A returns false.
    – Wen
    Commented Apr 22, 2014 at 7:07
  • what about kind_of? Commented Jul 28, 2014 at 20:49
  • 1
    kind_of? tests whether an object is an instance of a class. Not whether the object inherits from a class.
    – Confusion
    Commented Jul 29, 2014 at 6:18
  • 6
    kind_of? is an alias of is_a?
    – coreyward
    Commented Jul 19, 2017 at 2:08

2 Answers 2

402

Just use the < operator

B < A # => true
A < A # => false

or use the <= operator

B <= A # => true
A <= A # => true
9
  • 14
    @Brian Because is_a? translates to is instance of. B isn't an instance of A, B.new is though (B.new.is_a? A # => true). Commented Oct 24, 2012 at 7:44
  • 6
    Hmm, strange syntax (wouldn't have been my first guess), but thanks for the clarification! Commented Oct 26, 2012 at 1:31
  • 2
    For documentation see Core API / Module / #<.
    – webwurst
    Commented Feb 15, 2013 at 15:22
  • 2
    My love/hate relationship with Ruby continues… Why provide a different function for an operator used for declaring class relationships AND provide two different ways of doing it?
    – Ben Gotow
    Commented Apr 16, 2013 at 15:08
  • 7
    @BenGotow - 1. Because < is not an operator, it's a method. 2. Because < only checks for a subclass, and <= also includes self. Commented Nov 28, 2013 at 12:03
69

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
#=> false

B.ancestors.include? B
#=> true

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

10
  • 26
    More readable: B <= B (same result as B.ancestors.include? B). Commented Dec 28, 2010 at 14:49
  • 1
    Update: The immediately preceding solution works with 1.9+ whereas there is no "ancestors?" in 1.9.
    – user246672
    Commented Nov 14, 2011 at 6:40
  • 9
    This will not confuse people not familiar with the '<' syntax, and for that reason I prefer it. Commented Nov 18, 2013 at 14:42
  • 2
    @SimonLepkin Probably not "a while", unless you can experience microseconds ticking by. ;) Yes, behind the scenes the include? and < methods loop through the ancestor chain. It does this in C, so surely faster than looping through the Ruby array...but practically the two should be indistinguishable.
    – Phrogz
    Commented Jul 10, 2015 at 22:36
  • 2
    @JunanChakma Based on how the English word "ancestors" is defined, I agree that the return value should not include B. But it does. The method documentation says, "Returns a list of modules included/prepended in mod (including mod itself)." (emphasis mine). I'm guessing it includes its own class for convenience when using .include?, but that's just speculation on my part.
    – Phrogz
    Commented May 7, 2018 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.