Nick Ginanto
Nick Ginanto

Reputation: 32120

How to know what the inheritance line of a class is in ruby

if class C < B and class B < A

is there a command to know that C inherits B which inherits A?

Upvotes: 1

Views: 62

Answers (3)

philant
philant

Reputation: 35806

You can use is_a? to test if an object inherited from a class.

class A
end

class B < A
end

b = B.new
b.is_a? B   # true
b.is_a? A   # true

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

Are you looking for this?

class A; end
class B < A; end
class C < B; end

C.ancestors # => [C, B, A, Object, Kernel, BasicObject]

Upvotes: 3

user1790619
user1790619

Reputation: 510

Class.superclass

class A; end
class B < A; end
class C < B; end

C.superclass            # => B
C.superclass.superclass # => A

Upvotes: 1

Related Questions