C.J.
C.J.

Reputation: 16081

Ruby: How to find superclass of an object?

In ruby I can find the class of an object by calling the 'class' method:

5.class
'foo'.class
3.14.class

How do I find the superclass?

Upvotes: 1

Views: 954

Answers (3)

usha
usha

Reputation: 29349

this should work

5.class
=> Fixnum
5.class.superclass
=> Integer

You can also use ancestors method which will give you a list of all the superclasses

 5.class.ancestors
 =>  [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]

Upvotes: 4

rastasheep
rastasheep

Reputation: 11212

You can use .superclass like:

3.14.class.superclass

More info about that mhetod you can find here.

Upvotes: 3

ptierno
ptierno

Reputation: 10074

You call the superclass method on the class

2.1.2 :003 > 5.class.superclass
 => Integer

Reference

Class#superclass

Upvotes: 2

Related Questions