Home > Programming > Understanding the Ruby object model

Understanding the Ruby object model

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.

Objects

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.

Classes

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.

Relations

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.

Superclass/subclass

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
end

class Dog < Animal
end

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.

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

    Class.class.class.class.class.class.class.class

  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
    end
    

    :)

  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.

    http://en.wikipedia.org/wiki/Turtles_all_the_way_down

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

    @Skilldrick

    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. November 10th, 2014 at 00:55 | #22

    2014 May; 210(5):489-90. Epub 2013 Dec 12.Interventions at home births. [Am J Obstet Gynecol. The past ten years have Veste Barbour seen the elucidation of the molecular basis of insect resistance to many chemical insecticides. Target genes, mostly in the nervous system, have been identified and cloned from Drosophila melanogaster and resistance-associated mutations have been examined in a range of pest insects. More recently, with the advent of annotated insect genomes, resistance mediated by c

  23. November 10th, 2014 at 01:00 | #23

    A Marcha (pense Bear Sterns) e setembro (lembre-se do Lehman Bros) parcelas foram particularmente revelador a este respeito, o que corresponde, com algumas grandes falhas institucionais e casos graves de contágio mercado. Não, não comunicados de imprensa ou grandes campanhas de fantasia, mas uma boa pessoa para Oakley Portugal pessoa à moda antiga simpatia. Vamos comercializar este hospital psiquiátrico ao ar livre como um paraíso turístico / colarinho branco d

  24. November 12th, 2014 at 00:58 | #24

    [12] Sie erklärte, sie die Entscheidung, sie aufzugeben, machte Kind, weil sie ‘indoktriniert zu glauben, dass ich nie meine persönlichen Wünsche setzen vor das Erreichen des Zwecks der Sea Org’. Es gibt auch spezielle Regeln für saisonale Mitarbeiter, die 120 oder weniger Tage in den year.Determining Stunden von Longchamp Taschen Service, Polo Ralph Lauren Parndorf Vollzeitstellen und Durchschnitts WagesThe Regeln definieren Stunden-Service sind unverändert zu arbeiten.

  1. August 9th, 2011 at 17:08 | #1
  2. September 23rd, 2014 at 09:00 | #2
  3. October 22nd, 2014 at 22:08 | #3
  4. October 28th, 2014 at 10:37 | #4
  5. October 28th, 2014 at 15:36 | #5
  6. October 29th, 2014 at 00:00 | #6
  7. October 29th, 2014 at 18:05 | #7
  8. November 4th, 2014 at 14:55 | #8

Comments parsed as Markdown.