All I'd Wanted To Know About Ruby's Object Model Starting Out...And Mooar!!!

Summarized using AI

All I'd Wanted To Know About Ruby's Object Model Starting Out...And Mooar!!!

Tan Jun Qi • October 13, 2017 • Selangor, Malaysia • Talk

In the presentation titled "All I'd Wanted To Know About Ruby's Object Model Starting Out...And Mooar!!!", speaker Tan Jun Qi discusses the intricacies of Ruby's object model, aimed at beginners and those curious about Ruby programming. Tan begins by sharing his journey in learning Ruby, highlighting the initial excitement followed by confusion when confronted with advanced concepts such as singleton methods, metaclasses, and modules. He weaves an engaging narrative that personifies objects and classes, framing it as a creation story of Ruby's object model.

Key points discussed include:

  • Beginnings of Ruby's Object Model: Tan describes the evolution from procedural coding chaos to a structured object-oriented paradigm, introducing the hierarchy of Ruby's objects: BasicObject, Object, Module, and Class.
  • Existential Questions of Objects: Through the tale of objects like Doge, Tan illustrates how objects question their identity and hierarchy, leading to an understanding of their classes and superclasses.
  • Metaclasses and Singleton Classes: The speaker delves into the concepts of singleton and metaclasses, explaining how they allow for unique behaviors in objects, including the creation of class methods through a detailed analysis of C Ruby's source code. This includes a diagrammatic representation of the relationships between classes and their metaclasses.
  • Including Modules: The presentation explains how modules function within Ruby's object model, particularly how they are included in classes and how the inheritance chain is modified as a result of including multiple modules.
  • Comparison with Python: Towards the end, Tan draws a comparison between Ruby's object model and Python's, emphasizing their structural differences. He highlights that while Ruby employs a complex meta-class system, Python utilizes a single metaclass named "type" and supports multiple inheritance, showcasing the functional versus object-oriented orientations of both languages.

Concluding his talk, Tan stresses the importance of understanding Ruby's object model to aid in reading and writing Ruby code more effectively, encouraging intellectual curiosity among programmers. He suggests that knowledge of the object model can illuminate complex programming patterns and strengthen one's coding skills in Ruby.

All I'd Wanted To Know About Ruby's Object Model Starting Out...And Mooar!!!
Tan Jun Qi • October 13, 2017 • Selangor, Malaysia • Talk

Speaker: Tan Jun Qi

Website: http://rubyconf.my

Produced by Engineers.SG

RubyConf MY 2017

00:00:06.460 alright hello everybody and yummy yeah so I'm really happy to be speaking here
00:00:13.070 today my name is Jin C and just so you know I flew all the way from Singapore
00:00:20.360 to get here man so I am currently suffering from massive massive July ok
00:00:25.820 just so you know and just a disclaimer if you find any of my slides with on really silly it's not because of the
00:00:32.899 jetlag it's just because I'm a really silly person so you can tell because this is my github username I currently
00:00:39.890 look like this right now because my github student discount coupon expires next week cry and basically right now to
00:00:47.840 use a Singlish term derives from a Malay word I am late packing before I start work although the more accurate
00:00:54.379 depiction of my everyday right now looks something more like this strange to say that at a tech conference but it's true and you know so yesterday
00:01:02.420 actually I was furiously drawing and I was also paying attention but yes I was
00:01:09.530 drawing so that's why I'm still alive ok so my call today is called oh it wanted
00:01:16.670 to know about Ruby's object model starting out and more now I have to say this is the almost exact same talk I
00:01:23.660 gave her every Daughtry conference in June this year so if you were RDR see I'm very sorry you have to spend the next half an hour listening to me talk
00:01:29.120 about serious things we've silly drawings again but you know just before diving into the topic proper I want to
00:01:35.300 talk a little bit more about my motivations behind this so why this topic and why this talk so if you ask me
00:01:41.270 what is thought well the first thing I'm going to say to you of course this 3rds a ticket yay and although I complete
00:01:46.280 wholly they're not expected about three rubicon for my ticket yay but of course I want to approach this also as someone
00:01:52.850 who's there's a lot to learn about the language and programming in general so the question I have is what does it like
00:01:58.220 to be a beginner learning Ruby and I'll just give a brief outline of you know my personal experience so in the beginning
00:02:05.000 everything is great right languages clean powder free expressive sometimes even reads just
00:02:10.250 like English and blocks procs numerators lambdas are slightly strange but you get the hang of
00:02:16.250 it after a while and the base of the mechanics of the basic object-oriented paradigm under hard to pick up and
00:02:21.950 pretty soon you're writing your own classes subclassing them mixing in modules and so you are in love but if
00:02:30.650 you hang around with Ruby long enough you start to see fun stuff like this and
00:02:35.660 this and hear about singleton methods metaclasses extending modules instead of
00:02:41.390 including them and so on and so forth and a lot of head-scratching ensues and maybe you keep reading and rereading
00:02:47.300 Ruby Docs or various blog posts but you still can't quite figure out the difference between class eval instance evil or kind of quite you don't
00:02:53.930 understand what a self-locking could hook is doing down there and you decide that you should be able to better you
00:02:59.959 should be able to build a mental model there's the explanatory power of unifying all these disparate bits of
00:03:05.390 Ruby and so that is basically what I set out to do and that's all I want to know about movies object model starting out
00:03:11.470 but because I'm also a freaking completionist I decided to dive into the C Ruby source itself and that is where
00:03:18.079 the more comes in so without further ado
00:03:23.470 here is all I wanted to know about Ruby's object model starting out as a story so in the beginning there was
00:03:32.500 chaos but soon from that primordial soup of procedural code there sprung for type
00:03:39.049 deaths and macros and these gradually coalesced into the finest of all Ruby's and Ruby said let us make objects but
00:03:47.870 not in our image in our likeness for I am a jealous Ruby and I want to be the shiniest Ruby there is and so is wrought
00:03:53.510 the plainness of all objects basic object and basic object new kernel and unto them was conceived and born object
00:04:00.980 and object begot module and module begot class now Ruby had given her creations
00:04:08.060 dominion across the land and so object set forth and we got many other classes whose multitudes of concrete instances
00:04:14.540 soon spread across the code of the very many Ruby programmers in the world and so this was the world that all the
00:04:21.560 objects knew and it was great and happy but Ruby had also given her creations a
00:04:28.820 very special kind of power which was soon to precipitate the great existential crisis and this was the
00:04:35.180 power of introspection and so it was that one of the first objects dodge
00:04:40.850 began to ask what am I and he discovered
00:04:46.130 he could call the method class and the answer was as plain as day he was a dog and so the existential crisis passed now
00:04:55.610 Doge was happy but I was not long before dogs began to ask the same question so he asked what am i and so he called the
00:05:01.610 method class and discovered her what he was was a class and he discovered he could also call the method superclass and discovered that his parent was none
00:05:08.390 other than object and so dog knew what he was and where he came from he was content yet this was not the end soon
00:05:16.730 even the most ancient of objects began questioning their own existence basic object object module class all of them
00:05:24.950 asked what am i and it turned out that all of them were classes and they
00:05:32.030 remembered whom had begot whom Colonel asked herself the same question
00:05:37.190 she discovered it she was a module and remembered that she had no parents to speak off and so this was the world that
00:05:43.460 all the objects knew and some of them thought the arrows were getting a little bit messed up buddy with of it last the
00:05:50.300 first wave of the existential crisis was now over but soon enough Doge began
00:05:55.850 agonizing again he complained to dog you said it as a dog I should be able to
00:06:01.160 bark and wag my tail and be stroked on my belly and so forth I know that I'm different from the other dog instances I
00:06:07.790 weigh different and so on but truly I want more individuality than that I want the means and the methods of manifesting
00:06:14.600 my singly Doge nature not just to bark but to go so doujin such Wow and dog
00:06:21.590 looked at him and shook he said for him you know what he could do but in the night Doge was visited by
00:06:28.320 Ruby herself who was full of sympathy for the poor animal and uh she spoke
00:06:33.410 that's the I knew grant you the power to be the Doge that you want to be no longer shall you be dog but you shall be
00:06:40.230 a singleton Doge you had to keep the peace I cannot make this obvious for if I do dog will be jealous and so always
00:06:47.400 created a new class the singleton class of Doge but it was such that if Doge caught the method class he knew himself
00:06:54.600 still as a dog it was only recalled the method singleton class that he knew is uniquely Doge abilities came from only
00:07:02.490 Ruby knew that deep in the primordial chaos though just true class was actually his singleton class and so now
00:07:10.350 Doge was happy but now dog had a problem he prayed to Ruby the program must want
00:07:17.220 me to keep track of all my dog instances and find them by the name I can't do this with normal instance methods I need
00:07:23.160 class methods and Ruby remembering what she had granted Doge saw a similar solution here and so she spoke fastly I
00:07:31.320 do grantee with power to have methods of your own and not just methods that all classes possess and so was created a new
00:07:38.430 class the singleton class of dog but because dog himself was a class this singleton class was like a class of a
00:07:45.150 class and so ruby kristen it a meta class to distinguish it from the singleton classes of ordinary instance
00:07:50.430 objects like Doge but in creating the Dark Matter class Ruby now had to create the class meta class and a monumental
00:07:58.200 class and the object meta class and the basic object metal class and she made it such that the original genealogy was
00:08:04.020 mirrored so it was as if the basic object matter class had be got to the object meta class and the object metaclass had be got the moderator class
00:08:09.840 and because we love arrows she made it as if class had be got the basic all
00:08:15.270 during that class and so finally the objects and classes were finally happy although now their
00:08:21.900 world has been very much complicated and every so often one of the meta classes would be wanted to cause mischief
00:08:27.360 demanding that they have their own meta class and these meta meta classes would be even more mischievous demanding that
00:08:33.390 they have their own meta class in on and on with these metal metal metal metal metal metal classes go until no one
00:08:39.419 could see where the whole damn thing and and the ordinary objects and instances just wrote their eyes and carried on the
00:08:45.510 day-to-day oblivious to all this metal madness and that is pretty much where my
00:08:51.600 Ruby creation story ends so hopefully that was able to give you a sort of grand schematic overview of how Ruby is
00:08:57.480 object model looks like in a fun way so now I'm done done it is time for more
00:09:05.070 all right so it's time for me to retell the creation story of Ruby through the
00:09:10.350 lens of C UV source so as we as we know
00:09:16.050 in Ruby they say all data is represented as objects right so Doge is an object and dog which is also which is a class
00:09:22.260 is also an object now what this means in Serie B source is that all Ruby data is represented as pointers to Struck's so a
00:09:29.100 pointer is just this arrow you see over here just pointing to a value in memory address and that value happens to be a
00:09:35.070 struct struct it's just a bag of attributes or members so now the question of course is what is in this
00:09:40.800 struct or out of water in the different kinds of structs we have in C Ruby so there are three strokes were primarily
00:09:46.200 care about the first is our objects so this is the struck that represents ordinary instance objects second is our
00:09:52.589 class this is the stronger actually represents class objects and you notice that both of these strokes actually
00:09:57.870 contain our basic so that's the other strut we'll be looking at so let's look at our basic first this is the struct
00:10:04.410 that contains data that's basic to all objects right so we have flags this
00:10:10.140 member basically contains metadata so whether or not our objects are instance object a class object module object or
00:10:15.600 whether or not it's a singleton class that's all stored in flags and very importantly RIA this class
00:10:20.750 pointer here so this is a pointer to an art class struct which keeps track of what is the class of our object so
00:10:29.210 moving on to our object it's really just our basic plus stuff right and this
00:10:34.280 stuff here this Union thing you see over here it's basically a way of storing instance variables
00:10:39.310 finally our class again in as our basic so it has flags and class and very
00:10:46.100 importantly it has a super pointer so this is a pointer to another outlaw struct which is a superclass of my class
00:10:51.770 and next we have something a pointer to something called a Ruby class extension struct we won't really talk about it
00:10:56.780 much but suffice to say it star stuff like class instance variables class constants so forth finally we have a
00:11:02.510 pointer to the method table this is actually the table that gets looked up on method dispatch and so if you talk
00:11:08.330 about method dispatch this c function is where the keyboard happens over here in this for loop we're basically saying
00:11:13.730 keep searching the method tables of my supers until I find a method with the corresponding name and so if you call
00:11:19.850 the method class on the doge on doge this is the chain of our class trucks
00:11:25.550 whose method tables you will be looking up except not quite right you notice
00:11:31.520 there's a bit of a complication from kernel because if you realize kernel is not the superclass of object it's a
00:11:36.800 module that's mixed into object but somehow it finds its way into the super chain over here and you'll see how
00:11:42.350 ignore curly how that happens when we talk about modules later on so we've
00:11:48.140 talked about how ruby represents our data with the tree kinds of struck so now the big question is where does it all begin where does our creation myth
00:11:55.250 actually start and look no further than this giant function init VM object in
00:12:02.270 object C this is really really huge function but right at the top you see we are calling init class hierarchy and
00:12:07.550 this is the function actually where our class how he gets bootstrap so after all these boot def class methods or
00:12:12.950 hierarchy goes like this and after this these are basic set class macro calls we are actually setting the class of basic
00:12:19.700 objects object module and class 2.2 class and maybe at this point you're wondering
00:12:26.769 where is Colonel so go back to in a TM object jaian jaian gigantic function but
00:12:33.790 some wind we are actually defining Colonel and including it in object and so our diagram that looks like this so
00:12:43.029 this is pretty much what you saw from the creation myth just now in addition to basic objects object module and class
00:12:48.819 Ruby also initializes all the building classes like nil class to class string array and so forth and so now the next
00:12:55.720 interesting question is what happens when we actually define our own class so
00:13:02.079 let's try to create a dot class the function that gets called in C is this
00:13:07.329 or be defined class ID and it's basically doing three things first thing you notice is that it's actually setting
00:13:12.609 the super so if the super is not explicitly given we set the default stupid object so that's why all our
00:13:19.059 classes inherit from object by default next we actually initialize the new art class struct with the given super so
00:13:25.509 this actually sets the super to point to whatever super was determine previously and it also sets the class pointer to
00:13:31.449 point to class with a capital C and finally we actually make and set the
00:13:37.600 metaclass of our new class straight away okay so before the metaclass stuff ever happen
00:13:44.049 even happens this is how a diagram looks like so it's pretty simple right we just created a new class dot we set it super
00:13:49.749 the point object we set its class to point to class okay pretty simple but has before meta classes and before I
00:13:57.009 talk about my class creation I I want to clarify terminology a bit because I know this is always a point of confusion for
00:14:02.529 beginners as it was for me and I think this is worth mentioning so singleton class is basically synonymous of icon
00:14:10.179 class a meta class is a kind of singleton class but specifically the
00:14:15.189 singleton class of class objects so we speak of the metaclass of dog but the singleton class of Doge
00:14:21.870 and so because she realized that since singleton classes or meta classes are also classes you can have metal classes
00:14:28.350 of meta classes so meta meta classes and meta meta meta classes and in general you can have meta to the end classes
00:14:34.290 although if you think about it like any end greater than one is practically useless right but it's just kind of fun
00:14:40.320 to realize that Ruby's object model actually allows you to do this ok so let me talk about meta class creation so
00:14:47.880 Boromir knows well that one does not simply make a meta class and indeed if you look at this make meta class
00:14:54.300 function it is not the easiest to digest but let me break it down for you again
00:14:59.430 it does simply three things so the first is that we actually initialize our new
00:15:06.240 meta classes and you are a struct secondly we need to set the class
00:15:11.339 pointer of dog or original class to point to this new meta class and then we need to set the class pointer of our
00:15:17.700 meta class to point to something something to do with this engine class temp macro you see here and finally we
00:15:26.610 need to set the super pointer of our meta class to point to again something
00:15:33.260 so let's see diagrammatically what happens when my function is executed up to this point
00:15:38.339 right before we actually call this funny macro so up to this point it just looks like it's pretty simple we created our
00:15:44.640 new single to metal class I was in Godin class we set the class pointer of dog to point of it and now we need to set this
00:15:51.060 class pointer to point to something okay so what is that something go back to our function this something has to do with
00:15:59.100 this and sure I own class macro call and basically what this returns at the end of the day is the meta class of class so
00:16:07.550 the super class the meta class the class of dogs meta class is going to be the
00:16:13.200 matter class of class and if the metaclass of class doesn't already exist we have to go and make it right so we
00:16:18.900 have to call make meta class of class so let's make the class better class of class its again it's the same function
00:16:24.720 let's make Matt a class so that we do the same thing right we initialize the new our class struct and then we meet
00:16:29.970 the same problem we have to set the class pointer of the metal class of class now you notice that for the metaclass of
00:16:35.060 class we actually execute a different branch of the if-else conditional and so this line basically says that the
00:16:41.090 metaclass of that the class of the metaclass of class is itself right so
00:16:46.160 the class point that points back to itself and our diagram looks like this so this class pointer points back to
00:16:52.070 itself so now we're happy of the class point about what about the super pointer
00:16:58.780 well let's look at the function again notice this line of logic demo
00:17:04.100 worried about and the logic of this line is basically that the super class of
00:17:10.390 classes metaclass is the meta class of classes super class or in other words
00:17:16.480 the super class of classes meta class is the meta class of module so it's like
00:17:22.280 we're kind of setting off a chain reaction over here right because now we need to set the super class of the meta class or module and by the same logic
00:17:28.250 that's going to be the meta class of object and then by the same watering the super class of the meta class of object
00:17:33.440 is going to be the meta class or basic object and so we've kind of reached the end of the chain so what should the
00:17:38.720 super class of the metaclass of basic object be well if you go back to the
00:17:43.850 function again it turns out that it's just gonna be class and so you thought
00:17:50.150 we were done but actually we're still not done creating the metal class of dog right you just completely sidetracked creating all those other meta classes
00:17:56.360 just now so let's go back to the metaclass of dog and finish making it so now the last thing we need to do is to
00:18:01.850 set the superclass of the metaclass of dog and again it's the same line of logic and by the same logic the
00:18:08.030 superclass of the metaclass of dog is going to be the metaclass of the superclass of dog in other words
00:18:14.270 the superclass automatic last dog is going to be the meta class of object and so congratulations we are done so all
00:18:21.200 you wanted to do was to create one class where we end up spawning like five other classes along the way and gazillion
00:18:27.380 arrows so indeed Boromir was right but anivia at this point you're like oh
00:18:32.570 my god this metaclass stuff is insane right but fret not because after this
00:18:37.760 making singleton classes is a walk in the park and you can compare the size of the functions so this is the function to
00:18:44.480 make a singleton class getting pretty Street for straightforward and the street things first we actually sync
00:18:49.520 initialize a new art class struct for our new singleton class and important thing notice is that the super of my new
00:18:56.660 singleton class is actually the original class of my instance object so if I make a singleton class of doge the super of
00:19:02.030 the singleton class is going to point at all and next we actually set the class
00:19:08.060 pointer of doge to point to this newly initialized singleton class and finally
00:19:13.400 we need to set the class point of our newly initialized singleton class to point to the metaclass of class and so
00:19:21.350 at the end of the hour our game looks something like this and this is pretty much where our creation myth end at all
00:19:27.850 but you know at this point if you've been paying attention you realize that we are still missing quite a big part of
00:19:34.520 Ruby's object model right and that is basically modules so how does including
00:19:40.670 modules actually work and the answer is that Ruby finds a very clever way of
00:19:45.710 sneaking modules into our inheritance chain with something called include classes so I want to give Doge some say
00:19:55.280 Bertie is cool right so include the same atif module into the doll class and what
00:20:00.950 happens when I do this is that Ruby's gonna create this saber teeth include class that you see here and it's going
00:20:08.210 to insert it into the inheritance chain just like that so now the support dot points to this include class and now the
00:20:13.940 interesting thing about this include class is that it actually shares the same method table as my module and so in
00:20:19.400 this way method dispatch this works like normal right we just go out and hurt and shame okay so that was pretty straightforward
00:20:26.270 and this is where the this is a c function right above the logic because this is a very long and convoluted
00:20:31.970 method i admit i probably don't even really understand it but this is the main part where we are creating the new
00:20:37.160 include class and you're inserting it into the Harringtons chain with our class set super
00:20:42.700 now I should mention that because mod use and include classes they are just our class trucks right and our class
00:20:49.010 trucks and end of the day they're just going to have this class pointer so we know the class pointer of modules they
00:20:54.590 just point to Manu with a capital M so the class of say buddy's modules module but what about a class of our include
00:21:00.470 classes once you can see from this diagram the class point offer include classes just points to the module it was
00:21:05.660 created from and this is how our include classes actually keep track of the modules they were created from ok let's
00:21:14.480 talk about a very common scenario including multiple modules so sabre thief is not enough right make those
00:21:21.050 even more kick-ass I give him sunglasses yes so I include saber thief and then I
00:21:26.390 include sunglasses in the dog class and what happens when I do this is that by the logic of how the include modules at
00:21:32.720 function works inserts include class inter inheritance chain the module that's included later is basically
00:21:38.420 insert a load on the chain so in other words the method table gets looked up first okay
00:21:44.510 so that was fairly simple let's talk about something not less simple but more
00:21:50.390 interesting what about including modules and modules so at this point I should
00:21:56.180 say again include classes and modules address our class drugs right and other than the class pointer our class drugs
00:22:02.930 also have a super pointer and if you've been paying attention you may be at this point be like wait a second can modules
00:22:09.530 actually have a super ok right if you think about it you don't really think of what is having a super class right but
00:22:16.580 the answer to this is actually yes monies can have a super but not by default okay by default the super
00:22:22.220 pointer is actually no but this super pointer actually comes in handy when we include modules and modules so let's
00:22:30.260 take a look at how that works so instead of giving Doge saber teeth and sunglasses I
00:22:38.820 want to give him saber chief sunglasses ok so I include the saber thief module
00:22:44.130 in the sunglasses module and then I include the sunglasses module in the dark class all right so saber suncast Doge so when
00:22:51.600 I include this a belief module in the sunglasses module what happens is basically it's not very very different from including a module in a class right
00:22:57.780 I actually create a saber teeth include class I set the super pointer of my sunglasses module to point to this
00:23:04.170 include class and then when I finally include my sunglasses module in my dog class I end up getting to include
00:23:10.050 classes inserted into my inheritance chain just like this and then method dispatch works like normal alright cool
00:23:19.830 so I've covered actually a lot of stuff right I talked about how objects and classes are represented in C Ruby I talked about how singleton classes and
00:23:26.370 meta classes are created and I thought about how Mahdi's work so before moving
00:23:31.470 on to the next part of my talk I would be remiss not to clarify these differences between the C class and
00:23:37.110 super pointers and the Ruby class and super class methods I'm sure you're wondering if that's why there's a discrepancy between the two and it's
00:23:44.340 really not very difficult so think about Ruby's class method is that yes it basically follows this c
00:23:50.520 class pointer but it just kind of ignores any singleton classes and include classes along the way so if I
00:23:55.650 try to find the class of dog I'm gonna follow this pointer but ignore all this stuff in red and I'm gonna come back
00:24:01.260 hey the class of dog is gonna be class according to Ruby in pennies and what
00:24:07.590 about super class it's essentially similar so the super class does follow the super pointer stolen SUV but it
00:24:14.790 ignores any include classes along the way so even though the super of dog is actually the saber teeth include class
00:24:20.460 over here the super class of doll is still going to be object
00:24:26.050 okay so this is essentially where my talk ended when I give it an RDR C but
00:24:32.180 since I was given a slightly more time for this conference about five minutes I decided a little bit more content yay
00:24:39.700 so I thought that the best thing for a talk like this would be to like kind of
00:24:44.990 like compare the Rubies object model to an object model of another class based over P language right and so get ready
00:24:52.040 because it's Monty Python's Flying Circus so yes I will be talking about
00:24:59.770 Python specifically Python tree and I really don't have that much time so this
00:25:06.020 is only just gonna give you I just hope to give you a little bit of taste of how different the object models of both languages actually are and hopefully for
00:25:13.280 those of you blow any Python this won't complete fire your head but for those of you are familiar of both languages who I hope you find it interesting so the
00:25:19.850 first thing we should take a look at is what like schematically how are the object models different right and by now
00:25:26.510 I hope I hope you're all familiar rubies this object class diagram over here and this whole multitude of metal classes so
00:25:32.570 now the question is how does pythons look like and the first thing you notice okay I'm sorry I was just really lazy
00:25:38.720 drawing these diagrams but the first thing you notice is that pythons diagram just looks way simpler right and the big
00:25:46.670 reason is that Python really only has one metaclass it only has one class that
00:25:53.870 is a class of other class objects and the name of that class is type so that's one reason pythons object model seems to
00:26:00.110 look so much simpler and even though you could actually create more metal classes
00:26:05.180 in Python by sub classing type like you see here these metal classes are more integral to pythons over P the way that
00:26:12.080 singleton classes and metaclasses are to Ruby and another huge difference between
00:26:17.180 the two languages is that instead of using modules for mix-ins like Ruby does Python actually does multiple
00:26:22.670 inheritance so if you look at this Python code over here the dog class inherits from canine and rabies carrier
00:26:29.480 whereas the Ruby snippet dog inherits just from canine and includes the rabies carrier module
00:26:36.750 so thinking about differences between these two languages it kind of brings up
00:26:41.860 a lot of interesting questions right because if you think about it the whole reason why Ruby had this crazy singleton
00:26:47.530 class meta class structure we saw we could have stuff like singleton methods and by extension class methods and if
00:26:53.890 Python doesn't have any of that you know then how does it do it how does it how does Python do stuff like class methods or static methods or even the equivalent
00:27:01.660 to Ruby singleton methods on instance objects and I never knew answers to any of these questions when I was learning
00:27:07.360 Python in school on my own and I never even thought to ask any of these any of this because I essentially never any had
00:27:13.840 any conception of how object models even worked before touching Ruby and I did a
00:27:19.300 reason why I got I started poking around when I started learning Ruby it was because anything about it like Ruby's
00:27:24.520 object model has this very strange recursive sort of elegance to it which I felt was almost intuitive in a way
00:27:31.410 anyway you know back to Python I discovered we're researching for this talk that Python does all this stuff
00:27:37.330 that Ruby does with meta classes with something called the descriptive protocol and I really do not have time
00:27:43.690 to do justice to this this gigantic topic in this talk but you know maybe I'll save it for all I want to know about pythons object model or something
00:27:50.260 probably won't happen but anyway the descriptive protocol is actually integral to the whole Python over P
00:27:56.380 because it is the whole framework that governs attribute lookup and by extension governs method lookup because
00:28:02.170 in Python methods are just function objects there are attributes on the class and like the more I thought about
00:28:09.340 this this distinction between the two languages and I hope I'm getting it right by saying this but as the more I thought about it the more I realized I
00:28:17.230 kind of boil down to this this big difference between the two right so I think that Python is distinctly a
00:28:22.720 function based language whereas Ruby is distinctly object base in the sense that in Ruby if you think about it like
00:28:28.750 there's no real notion of function that is not bound to an object right so if
00:28:34.780 you're aware there is an unbound method class but you cannot call an unbound method you have to rebind it to an
00:28:40.390 object before it can be called right so there's no notion of like a pure function object whereas in Python
00:28:46.360 it's the complete opposite right and Python actually methods are built upon function objects and I thought that was
00:28:53.320 a very interesting difference and I realized that as I thought about it that this difference was actually evinced by
00:29:01.740 this very simple syntactic difference between the two languages that anyone who has played around with both immediately notices right and that's the
00:29:08.470 parent theses behind the method calls so in Ruby as we all know the parent theses are completely optional whether you put
00:29:13.720 them there or not your function is going to get caught whereas in Python they are absolutely necessary because if you do
00:29:19.360 not put the parent DC's after the method call you're just going to get the function object you're not going to get the result of the function call and at
00:29:27.220 the beginning right when I just learned ruby invited and I thought that this was just oh this is just like a - and tightly difference right ruby index to
00:29:34.059 type this yeah or the parses are just different but then as I thought about it more I was like hey this this difference
00:29:41.019 in syntax this simple different syntax actually like a glimpse into how different the object models of both
00:29:46.149 languages actually is I thought it was pretty interesting so I talked about and
00:29:53.320 maybe at this point you're like oh yeah all this internal stuff so fun yeah but why why okay doesn't help me become a
00:30:00.519 better Ruby programmer I dated a does it well yeah maybe I admit that knowing
00:30:06.700 that I can create metal metal metal metal metal metal metal metal metal classes in Ruby is really not the most
00:30:13.389 useful thing in the world but you know two reasons right the first it's just so intellectual curiosity I hope you found
00:30:19.179 I hope you find interesting to like just have a better understanding of how Ruby's object model actually works and a
00:30:24.820 second is I do think that having a small solid understanding and even having an appreciation of how different kinds of
00:30:31.509 object models can possibly work will help you reason more clear you about your code right and so in events that
00:30:38.049 you ever meet any potentially confusing situations such as all of this there is
00:30:43.659 no reason for me to talk about it because I I don't have any time but because you now have the awesome mental
00:30:51.580 model to figure out all by yourself the mental model I wish I had starting out and more so thank you thank you so
00:31:05.389 much everybody please pick your jaws off of the floor she did draw all of those okay any questions that you have on the
00:31:13.279 Ruby object model and now by extension the Python object model nobody everybody
00:31:21.080 got that word-for-word promise come on
00:31:31.899 yes our sponsors question of the day
00:31:38.470 anything be you on source code you can recommend to read okay so something is
00:31:45.019 very helpful for me to actually understand the source code was this this online documentation online book called
00:31:51.830 the Ruby hacking guide is largely written in Japanese by Cranberries name
00:31:57.559 but it was only written for 1.8 who u1 about it but all the core stuff really hasn't changed that much so it should
00:32:03.289 really help you in reading the Serie B source is it translated English yes yes
00:32:12.710 and the other book is Ruby under microscope and that's the book that Sauron is reading with her book club so
00:32:19.070 you guys can join in on that any other questions oh of course
00:32:45.390 okay I'm sorry that's a very bad question to ask me because as my my one of my first lights showed is I don't
00:32:52.710 really program much nowadays because I'm wearing this that worked where I will start programming as a proper program
00:32:58.520 but I this definitely helped me understand other people's code much much
00:33:05.220 much more clearly because in a lot of like rubygems and stuff when you read a sauce people use a lot of meta
00:33:10.500 programming patterns that just come to me like what what are they actually doing and like having this kind of
00:33:17.010 understanding of the object model really helped me in reading reading and understanding that kind of code so in a
00:33:23.910 nutshell that was casual learning in her spare time amazing any last questions what do we
00:33:31.559 have for 20 yes Louie please so other than just the object model is
00:33:38.640 there any any part of Ruby where you where you would like to explore more
00:33:44.130 where you see a large gap in your mental model so I think for me it would be like
00:33:53.299 kind of like closures and the execution environment off of method calls because
00:33:59.790 I think that is also actually quite a big part of the object model right because knowing where you're from your
00:34:05.880 methods actually executed the scope they have methods executed and I think for me I that's something I complete and talk
00:34:11.970 about in this talk and to me be honest I'm not super clear on that at this point but yes that is definitely something that would be good for any
00:34:19.500 movie remember to look into all right thank you so much one more
Explore all talks recorded at RubyConf MY 2017
+16