Home > Programming > Understanding the Ruby object model

Understanding the Ruby object model

August 7th, 2011

Not a very long time ago, I found the Ruby object model very confusing. Part of my problem was that I was trying to model the relationships as a directed acyclic graph, whereas in reality, the model contains cycles. I’m guessing that others have found this confusing, so I’m going attempt to explain how it works in a way that my past self would have understood! Note that this is a basic explanation, and glosses over higher-level concepts like the eigenclass/metaclass (I’ll leave that for a later post).

Different things in Ruby

There are just a few things that exist in Ruby that you need to understand to really grok its object model.


Everything is an object. You can’t get very far into Ruby-land without hearing that phrase. It’s true though, everything in Ruby is an object. The interesting thing is how those objects are linked together and classified.


All objects are instances of a class. There are three special classes that are the key to understanding the Ruby object model: Object, Module and Class. More on them later.


Like I said before, it’s the links between the different objects that explain how the Ruby object model works.

Instance of/class

All objects have a class – they are instances of that class. That means that whenever you have a class, you can call .new on it to get an instance. And whenever you have an instance, you can call .class to get its class. This seems obvious, but it can get confusing when you remember that everything is an object, and so everything is an instance of a class.


All classes have a link to a superclass. If you don’t explicitly inherit from another class in a class definition, that class will inherit directly from the Object class.

How they fit together

Here’s some code to play with:

class Animal

class Dog < Animal

fido = Dog.new

Once you know what the pieces and the relations are, it’s fairly straightforward to piece them together. All classes are an instance of Class. That includes the class Class, so there’s a circular relation there (which isn’t a problem).

Dog will basically have the following inheritance hierarchy (the superclass links):

Dog < Animal < Object < BasicObject

(Ruby 1.9 introduced BasicObject as Object‘s superclass.)

The Class class has the following inheritance hierarchy:

Class < Module < Object < BasicObject

Anything that’s a class (so BasicObject, Object, Module, Class, Animal and Dog here) has Class as its class – they’re all instances of Class. Any other objects have their own classes. So fido above will have a class of Dog, 3 has a class of Fixnum, “an awesome string” has a class of String, etc. All those classes inherit from Object ultimately (although classes like Fixnum have their own complex inheritance hierarchies).

To finish things off, here’s a little diagram of what I’ve just described, showing all the class and superclass links.

The Ruby object model

Categories: Programming Tags: ,
  1. Marco
    August 9th, 2011 at 10:43 | #1

    What´s the point of calling a class of class? Like this circular thing, where class is a instance of class, why such thing should be important?

  2. August 9th, 2011 at 10:49 | #2

    @Marco A class is an object, so just like any other object it needs to have a class. If you have an object and you don’t know if it’s a class or not, you can ask it what its class is. The fact that Class is an instance of Class is just an interesting fact that arises from the model. It’s not necessarily going to be something you use every day.

    August 9th, 2011 at 13:49 | #3


  4. Name
    August 9th, 2011 at 13:53 | #4

    Looks like all classes originate in an intersection of blue lines.

  5. August 9th, 2011 at 13:54 | #5

    @STEVEN SEAGAL How about this:

    #is this object the Class class?
    def is_Class? obj
      obj.class == obj


  6. Name
    August 9th, 2011 at 13:55 | #6

    Oh wait. Sorry. Didn’t spot that small blue arrow pointing into Class. All classes originate in class, then. Classy.

  7. August 9th, 2011 at 13:58 | #7

    @Name Yeah, without drawing curvy arrows is was a bit tricky to make it obvious where all the lines went, but I feel like the text + the diagram conveys it.

    Also, classes are all instances of Class which makes sense if you think about it. They all inherit from Object though – the point of this article was to highlight the difference between the class and superclass associations.

  8. August 9th, 2011 at 13:59 | #8

    Much like turtles, it’s classes all the way down.


  9. August 10th, 2011 at 04:49 | #9


    So you’ve basically just re-implemented #instance_of? there, yeah? http://ruby-doc.org/core/classes/Object.html#M001032

    One is ordinarily more interested in `#kind_of?` instead (if one can’t just pure ‘duck type’), which will return true if the object is an instance of the argument OR is an instance of a class which has the argument as one of it’s superclasses. `#kind_of?` will work on Modules too.

    You could also re-implement `#kind_of?` yourself, using the `#class` and `#ancestors` (http://www.ruby-doc.org/core/classes/Module.html#M000475) methods, which would be an interesting exersize to make sure you understand what’s going on.

  10. Devyn
    August 10th, 2011 at 05:00 | #10

    @Skilldrick You can use .instance_of? instead. For example, 2.instance_of?(Fixnum) returns true.

    If you want to know if an object could be treated as, say, a Numeric, you can use .is_a? / .kind_of?. They’re synonyms.

    2.is_a?(Numeric) #=> true, because 2's class inherits from Numeric.
    2.kind_of?(Numeric) #=> true
    2.instance_of?(Numeric) # => false, because 2 is not a direct instance of Numeric
  11. August 10th, 2011 at 09:11 | #11

    @Jonathan Rochkind No, that code snippet was a joke really. Look at the capitalisation in the method name: is_Class?. The method checks to see if its argument is the Class class, as that is the only object in Ruby whose class is the same as itself (obj.class == obj). It’s not at all useful, just thought it was amusing :)

  12. August 10th, 2011 at 09:15 | #12

    @Devyn Absolutely – it’s much better to rely on instance_of? or duck typing than explicitly checking the class. I was just showing how you’d use the .class relationship if you wanted to.

  13. August 11th, 2011 at 17:54 | #13

    I think it would help the diagram a to see the arrow coming out of the blue line on Fixnum. As is, it looks like you’re saying Numeric, Object, and BasicObject are instances of Fixnum. In reality, the line doesn’t go straight past the line to Class, but instead joins it, but since there is no arrow on the other side, this must be determined by realizing the graph doesn’t make sense and thinking about it a bit.

  14. alexey
    August 13th, 2011 at 10:16 | #14

    @STEVEN SEAGAL that’s very classy :)

  15. October 9th, 2014 at 01:59 | #15

    (Einige Luftschiffe haben auch Wetterradar, und Instrument für die Nachtflug bewertet.) Blimp Piloten FAA für leichter als Luft (LTA) Handwerk zertifiziert. Kaufen die besten von Jordanien Performance-Schuhe für Männer, Frauen und Kinder auf der Ziellinie, ist Longchamp Tasche Falten der Grund, warum wir so sagen, sind wie folgt. Aus der Tasche Grenzen kommen, dass sind entworfen, um Patienten aus gehen bankrupt.But zu schützen, wenn die 1 Million Amerikaner, die bisher

  16. October 9th, 2014 at 01:59 | #16

    I blog about homosexuality since it is part of the lifestyle that The almighty saved me from, together with seeing all the confusion as well as deception surrounding the issue, I like my words to be a supply of clarity and truth.3) Do you ever get nervous talking about a topic that seems to be a new controversial subject? If so, how would you handle it?Nervous authoring it, YES! But only since i don’t want my words to generally be taken the wrong way and become the stumbling block for Fake Oakle

  17. October 15th, 2014 at 01:59 | #17

    Vi har en $ 1m Challenge ut just nu, som handlar om att hitta en biomarkör som kommer att bidra till att utveckla ett botemedel mot ALS (Lou Gehrig sjukdom). Vanligtvis är det första pappor säger när de kommer ut ur förlossningsrummet “Barnet och min fru är bra, det är en flicka.” . Förutom din Nike Air Force Billigt förvåning besöker du kunde behandla dem till en måltid och dryck med restaurang, bar och caf kuponger.. Inte få

  18. October 20th, 2014 at 02:00 | #18

    “Il est vraiment la grande raison du festival jamais venu à être,” dit Bayfront fondateur Chris Mackey, qui permet de réserver Smith au bar de l’Harbor Inn à Duluth et a vu sa montée en popularité, planter une graine pour la fête. Il a dit Smith ouvre généralement le samedi gamme à midi: “C’était un bon réveil, et il aurait un grand nombre de personnes de sortir tôt.”. Les japonais ne pouvais couler le cuirassé, mais un livre

  19. October 27th, 2014 at 00:54 | #19

    Mayo verhoogde de lengte van zijn podcasts door herbestemming materiaal . In tegenstelling tot wat velen denken, een podcast maakt Hollister Antwerpen moet 30 of 60 minuten lang zijn. Warme comfortabele winter schoenen voor binnen-en buitengebruik. Hun financiële Hollister Online planning diensten omvatten beslissende beleggingsoplossingen voor niet-ingezeten Indianen, ideeën over beleggingsfondsen beleggen, verzekeringen planning, fiscale planning en meer. Die opbrengsten werden op hu

  20. October 28th, 2014 at 01:25 | #20

    Peut-être l’application la plus célèbre et commune de la théorie de la sélection adverse est dans l’assurance santé. Dit que nous aurons vos appels téléphoniques plus plus Aaron Hernandez qui obtiennent de nouveau dans la Red Sox de Rondo. Au moins, Hollister Belgique il ne parlait pas combien Hollister Anvers de personnes il avait tué ou combien de tirs à la tête qu’il a fait. Sa meilleure performance précédente était à

  21. October 30th, 2014 at 01:16 | #21

    WikiWelcomeTagsAll ArticlesRecent changesRSS FeedFollow em TwitterLinkedIn GroupPeopleSoft ServicesSearchScreencastsRecommended BooksContactMy BlogPartnersMembershipBecome um MemberContributing ao WikiForumsForumRecent postsHints, Dicas e GotchasAsk para Web HelpGeneral DiscussionEmployment OpportunitiesTop ArticlesApplication EngineAuditing Usuário ProfilesBatch SchedulingCampus Soluções TablesCI Desenvolvimento FAQComponent InterfacesCI Baseado ServicesConsuming um Web ServiceCo

  22. December 11th, 2014 at 09:29 | #22

    I blog about homosexuality since it is part of the lifestyle that The almighty saved me from, together with seeing all the confusion as well as deception surrounding the issue, I like my words to be a supply of clarity

  23. December 11th, 2014 at 09:30 | #23

    I think it would help the diagram a to see the arrow coming out of the blue line on Fixnum.

  24. December 11th, 2014 at 09:32 | #24

    Förutom din Nike Air Force Billigt förvåning besöker du kunde behandla dem till en måltid och dryck med restaurang, bar och caf kuponger

  25. Cary Swoveland
    January 30th, 2015 at 07:51 | #25

    You said, “All classes have a link to a superclass.” I suggest you make that, “All classes but one have a link to a superclass. The lone exception is BasicObject, which is a class, but has no superclass.”.

  26. May 7th, 2015 at 05:11 | #26

    El espectáculo de este monologuista gallego es parte integrante de ese reducido conjunto de espectáculos que nos atrevemos a recomendarte para tus acontecimientos.

  27. May 8th, 2015 at 12:07 | #27

    Bwin: Charlamos de una casa de apuestas deportivas austriaca con algo más de quince años de experiencia.

  28. May 29th, 2015 at 11:25 | #28

    Existen servicios de bicis públicas como en Madrid, Barna Vitoria que favorecen el uso de este medio de transporte ecológico y sano.

  29. June 10th, 2015 at 05:56 | #29

    I made a visualization of the ruby object model in case anyone is interested: https://mitya777.github.io/dev/2015/06/08/ruby-object-model/

  30. June 29th, 2015 at 04:55 | #30

    Thanks , I have recently been searching for information approximately this topic for a while and yours is the best I’ve discovered so far. However, what concerning the bottom line? Are you certain concerning the supply?

  31. August 14th, 2015 at 02:38 | #31

    Nun, wenn Sie eines meiner Blogs über die Beziehungen gelesen hast du, mein Motto ist POOF! Werden gegangen! Nike Günstig New York für schlechtes Benehmen. Das ist richtig, auf die nächste Mann in der Schlange.. Mein Draht ist 1,5 mm dick. Dies wird üblicherweise mit einer Zange erfolgen. Gesetz nicht im besten Interesse der förderfähigen Hausbesitzer vom 31. Dezember Frist registrieren lassen für die verspätete Anmeldung, aber es, die Steuerabteilung

Comments are closed.