Duration 42:07
16+
Play
Video

RailsConf 2019 - Zeitwerk: A new code loader by Xavier Noria

Xavier Noria
Ruby on Rails Consultant at Self-Employed
  • Video
  • Table of contents
  • Video
RailsConf 2019
May 2, 2019, Minneapolis, USA
RailsConf 2019
Request Q&A
Video
RailsConf 2019 - Zeitwerk: A new code loader by Xavier Noria
Available
In cart
Free
Free
Free
Free
Free
Free
Add to favorites
1.38 K
I like 0
I dislike 0
Available
In cart
Free
Free
Free
Free
Free
Free
  • Description
  • Transcript
  • Discussion

About speaker

Xavier Noria
Ruby on Rails Consultant at Self-Employed

Ruby on Rails specialist.I am a Ruby on Rails core team member. Author of the Zeitwerk Ruby library and Rails Contributors. Honored to have been presented a Ruby Hero Award in RailsConf 2010, and to be part of the team that organised the European Ruby Conference 2009 (EuRuKo 2009), held in Barcelona.Specialties: Ruby, Ruby on Rails

View the profile

About the talk

RailsConf 2019 - Zeitwerk: A new code loader by Xavier Noria


In this talk we'll introduce Zeitwerk, the new code loader for gems and apps that is going to be the default in Rails 6.

We'll cover what motivated me to work on it, which are the issues in Rails autoloading and why is it fundamentally limited, usage by gems, and interesting aspects of the implementation.

Share

So this is the menu that we have before lunch. We are going to interviews. What is divert the motivation that I had to work on this then we're going to see call Ray's Auto Loan Store Castleton seems beginning to compare that with the new solution and cause that interested in re6 because this is going to be the new world order by default in re6 All right. So what is diverted is Gem that provides auto loading loading loading of code? And very important because no dependencies. So Ruby is enough to solve these problems. And very important as well if it's been

designed to work with any Ruby project. So you might have heard of of it may be related to rain 6, but it is an independent designer to be able to work with any Ruby project in particular gems are your own private projects and it can be used by other one free words, so it's totally genetic inductance. Okay, let's see first how do we use this thing? And after that we're going to get a little more deeper. Took the premise to work with diabetes that you have projects tractor where find names match

constant pads that that's standard in rails. Okay, so it's like a very conventional project structure. I would say So for instance if we have used it, but they're being that you define the user constant, right user underscore profile. Should Define user profile. And if you have HTML parser the default constant named expected from that 5 would be a stable parcel with lowercase in in the industry letters. But if you prefer to have HTML like like like an acronym you can Define there's a way to

do that. Okay, you can you can you find a costume inflected for your loss. Says about everything by default IP illegal to throw to Super whatever, you know, but in the case of HTML police do a female in uppercase, there's people that is super super simple and deterministic different gems are different loaders have independent infected. There's no way that one can interfere with the other one did the Mystic and you have control about the way the five senior project are going to be in Flex. Then also very conventional

namespaces correspond to directories Rancho in Disguise from you since we have let's say our hotel class in hotel and then we have within quarter class where we have organized everything related to Bryson. Okay, so just you just have a hotel directory and then slice pricing is going on a suspected to Define Hotel Colin Colin price. I called this explicit name is Basis because we have also support for implicit name is spacious, which is something that race has had his the

beginning which is that if you are using directory just as an image space for instance here at Monroeville, but it's not a mother with with the stopping. It is just playing the role of a name is space. You don't need to be fine at me. Ever be as mean and only two that you don't need to do that range and you are not going to need to that also if using diaper. All right. Basic you said is very simple. You just spent a little older you say which out the root directory of your project like in rails that is going to be able to load and ruby gem is going to be leap that's

it. And then you can shut up and done that you are ready to do anything. Indication of gems since there is a common pattern for gems and projects that technically I'm not James you don't need to tell me about the structure similar to James. So this is just a shortcut to push lip as the only group directory of the project is very common, right and then also in James version. RV by convention is a constant where all the letters are in uppercase. So this this thing gives you also an inflected the fastest at the special rule for Bachelors to be

to use this in a jam. If you have alternate you can just listen to stand out what I mean, you use the gear the previous API and that's it. And the first thing the first the first call is on an Asian instance method is showing an instance method. The second one is interesting. It is a class method in diver clothes are the shakes at all. So the point of this one is Diaper Peeps are registry of all the lawyers that have been instantaneous. Okay, so, you know what service like in a race application, you know how

often times it's convenient production to load as much gold as possible on board. Okay. So if you are in a race application and there are 10 gyms in your dependencies. Are using diaper rash is going to race course this method okay to to to give you a concrete example and not only is Europe your race application going to be eager to load it but all the projects that are managed by Seibert. They independent lawyers are going to Eagle Road and all of them, right so you get all of that on the benefit in

the main process indeed. Even you can even have your ID application not using and driver because you can still Is the classical to do with race 651 but even if the the application itself is not just enjoy the interaction that we have written in Braille 6 is going to call this method in case you have dependencies that are using Diver by the way in re6 none of these API API because the racism integration that uses that the gist is API for you. So your interface in re6 is there still conflict. Outlaw past fanfic. Dash classes the same interface

that do you wear using to tweak? The stings is the one that you're going to juice and is interation is the one responsible for converting those stand-up Armitage engaged to the API off. In order to reload, you have to opt-in. Okay and discuss to proposes one is to a knowledge at the API level that reloading indeed is one of several possible use cases for this gem. So in general at a gym that is just a regular Library does not need to reload right because you have a gym and when you change something you may be right at the street, but you cannot have any service running. Okay. So in Amana

juice cases that I at least I am vision for the future I reloading this is not going to be used you're going to Auto load or a girl out in the Indulgence, but it's loading is is something that is going to be useful for things that are running like web applications. So by acknowledging this we are able to be also more performance 434 case, which is not reloading because reloading needs to to keep some Melody. No big deal. We can save so for the majority of juice cases that we save that okay, and

if you are in our in our framework and know that this is in addition, normally you only on on deployment show that the the combination is I am developing a service in development month. Okay for the rest of the day they're so if you if you see it with that perspective if this is just east of corner case maybe from JBL reloading so that reflects. Okay. All right. Why did I work on that? So three things I just might my first my initial motivation was the third one. So the result of other has some gorgeous. It has worked out very well for

many years and he has been very helpful, but there are some gorgeous that I can bring 6 to the technique that produces and and I wanted to to improve that in some in some way. And then I get something to work on this and I realized that I could also Target to pain points of mine a list to personal pain points which other requires in in in in auditory Ruby projects typically a burrito and a sense of a lack of brightness that I have some time so far up my project normally

follow these conventions. Okay. So if I have to use that the user plus I have to require user and for hotel have to require Hotel. I am I have a personal sense that I'm repeating myself all the time. I am using the consonant and then just lower case everything you require. All right, if we had like a good convention, maybe we could ultimate this. So that's a personal training for the people writing requires. It's fine perfect, you know, but this was a pain point of me wants to work on that. So why are requires

brittle I'm sure you have found these in any Brewery project except maybe a trivia Ruby project. But in any Rudy Project very easy to to forget to require so we have to be an example class airplane that includes local the ball that is not going to fly because I don't know what this constant. Okay? So you have to put a required for that you work? But the problem is that requires have you know, okay. And the consequence of that I'm sure you have faced this before

is that you could have the original source code without the required on this could work this will work because if in the gold pass someone else required that file local then when you arrive here since the constant of the volume is, you know in the in the state of the of the process of The Interpreter interpreter knows the motherless is already loaded so it works and the problem is that when you create a different Copart maybe production then you got an exception. So I am I ever spent a few a few

of hours in the race called Bay sometimes grabbing and looking for missing requires. Okay. So yeah, I don't know. I just I just a visit Britain. I just prefer not to do this if possible. So, all right. This is nano see which is a static site generator Nano C instead of doing like like cherry picking requires. It did something.. Sometimes Ruby projects also do which is in in the in the entry point of the gym. It has all these list of requires. Okay. This is like you got rolling for correct, so This also has

a cost. Because it cost because every time you add a file to the project to have to remember to go here and at the file to this to this list also, this list has to be kept order it and you may depending on which factors or something like that. You may need to reorder this because because if you have constant at the top level or class level they're going to you going to be able to resolve them when the files you get loaded so they have to be either loaded before so you have to maintain these by hand. Okay. So now I'll see you tomorrow using diver

everything. It's always delete, okay. So at dishwasher delete all spigot load done. So that's that that's the point you can streamline your perming knowing that your classes and mothers are just everywhere. That's it, you know forget about it for you if you want. So no, we are going to see ho rate as of 2044 and 2010 that I'm going to send the limitations of the technique of Rage. We're going to do a quick constant refresher to her after the concepts, you know, just praising mine and and be

able to to follow up with this light so many programming languages like that. We are doing accounting assignment, but they are a very rich topic with a lot of education on the staff. And the main reason is that Ruby does not have symptoms for PlayStation volume. So when we do this, we're doing a class at a constant assignment. So we think you know, what day today was thinking in terms of when we see that that we think in terms of classes, right? So that's the

same thing as doing this. You're doing stunts it a class object. That is a store in the sea constant are storage like variables like like this like dick sex store in one. There's no difference this. She's starting a class object. This guy is starting an integer object. This one is turning a class object that it and from here on constant and class model objects are independent. You can remove these constant. You can start the class object in the body of absolutely

orthogonal and that's what makes this this topic like no trivial in Ruby. So for instance here, we have you said the new er in terms of classes and mothers, but in reality what happens is that users are constant. That's an expression it evaluates to a class object and that object response to the new method. The return value of the new method is your user instance similarly in date. You have to sing at a constant forget about classes. A constant. That was a response to today. The return value of an ethical is a date optic that we started in the today variable. Okay

glass, you know, when we write the string that's that's built in the turbojet. It is already a memory when we both know. It's not the special at all. It's just a regular constant like x equals one, okay. Constance belong to Maldives So every class a model you have just you have to think that it has like a symbol table because like a hash table of constant names map it to the values. That's why I like literally that way. So every class a mother in your system crashed this little

collection Associated to it that the stores the constant. So when we do this thing plus hotel and his side that model pricing Hotel defines a constant in which class or module in object. So the top-level name is space is object. I am going to use the app that work name is space space is not a formal Concept in Ruby, but since we have we are working on the hypothesis that there's a project return we can work with a convention and we know what we mean when we say when we say you made me space in this context. Okay. So the name is Pace 444 hotel.

Where is Kordell Stewart? The constant is 13 object has a constant. A class object inside and it's stored in the collection of the object object is going to be Define it in the in the symbol table of the class storage in the hotel constant. Okay. So the the main characters Kia a class Emoji Logics Constance storage is the model of the class of Jack in this case within the hotel constant. That is just starting the pricing constant who's Associated value is a model object. Okay. That's the way works.

We can you have to have this done with the syntax. In this case hotel has to be already Define it as you know, okay, and we are defining or reopening a pricing constant with seeing the class. That is a store in the hotel. Okay, that's not the full thing. Any in in in in in this presentation is is an individual debut do not work. Maybe that way we can all sing, you know with this position. But for this presentation easy easy easy important. So we are going to see very quickly is not going to be reference. Just just a

reminder of how Constance are resolved in Ruby. We have this concept of nesting which is lexical basically like gave you every time you use the classroom. Are you are you are a nesting the class or more objects into an internal collection that The Interpreter maintains is that that's not visible you can inspect it, but it's not it's just electrical concept call it nesting. So this works doesn't seem to okay. So in the first comment I think that in the

line the nesting is formed by those two things Hotel pricing, which is a muggle object with which is the constant within constant, but that's at model of taking just one single Eastern direct and hotel which is observed that object is not part of the nesting you only do only push things to the nesting when you use the glass emoji keyboard, there are other ways to do it, but they are more scared not needed for this presentation. It is very interesting and very relevant to to constant reservation at in the

second case where we use the the fully qualified name. Let's say the nesting way we are only using one more you'll see were right. So that must be where is pushing that mother to the nesting touch it so hot Hotel pricing is in the nesting in both places by hotel is not when when when to look for the constants in the in the in the class of molecules is very important to understand that in the first case the constant if it's in the place of that comment is going to be checked in for the pricing and also in hotel where is in the second case it is not going to be check-in hotel because Hotel

does not belong to the nest. Then we have the concept of ancestors of our classroom Hollywood that you will know that is the ancestor of chain that we used to resolve methods, you know. This is an example with string. Okay, you can speak that as well. All right. So very quickly just to have an idea call with the results of relative constancy. So in this case, we think you're very funny stars in the jungle. Okay, so busy you are you are you know what you see the constant you want a certain place in the listing right? Like you said? Okay. So then

the season goes first or towards following the nesting from inside to outside. So first you go the Devil is a constant is not phone and you go back and go to the ancestors app right then. Object is check it later if if you are in a molecule because object does not belong to the nesting and does not belong to ancestors with Mother's. So if you use when you set the harsh constant, okay inside entire listing the first Ruby checks the nesting even if you seen her this is like the real thing as if it becomes built-in go

to check object, which is where I thought so it goes all this all this way until we find her, right? Finally is the constant is not found and there's a call back collect guns missing that is going to be called. There's a default implementation of calls missing, which is the one that racist name ever right number IDs. Then if you have a qualified constant, like a request here, this is much easier. So in this in this in rap request we have like rack the first the first segment that's a relatively Constant

Sorrow rock is going to be resolved with previous organism. Okay, that's that's relative. But request is qualified and request is the one that is solved using this disease organism, which is much easier it it just checked the ancestors of rack and what does a technicality. Jack doesn't matter and if it does not found confirmation for this is this is easier, right? And this does not take the nesting into account. Okay, so cold on Ray's Outlaw know that we

know this. I do know that can raise their business concept of Outlaw pass that by default has everything in a PP a PP Mother season of the World Part and the percent subject representative level name is space. Okay. And then race defines a confirmation cook. So when a constant is not found that dope clothing, right? The question is not is not following is it is a known at some at some place council meeting is called the rails knows that the constant with this name, which is a string. It was not found while

I was in this classroom audio range goes to the ultra low pass and see if one is it has to take us to underscore decals the name. Okay. So you got user Capital you you underscore that user and it goes towards the outlaw pass looking for looking for use your daughter be right if it finds it it loud thunder and execution resumes. are there many likes of quoting the bench is everybody so The problem with that is that the technique itself. So I've been able to work without

with a writing requires all these years. So this this was a feature that I love it I will race and it is that kind of feature that it's working for you and if everything is going well, you do not even notice you just it is just there is doing its work you don't even notice. But when when it does not work, sometimes it's difficult to understand why this documentation but there's some go charge so why it is limited incomes missing you get the constant name and and The Clash of Molly where

this was no phone. Which is the nesting of the point you going to know? It is not easy. It is not in the API icons missing. What's the constant midget in a relative or qualified constant contacts? You don't know so does not have the information to resolve the constant the way Ruby results Constance. Semantics do not match. It does the best that he can and you have so much emissions like I'm going to assume that the the name of the molecule is reflecting the next thing you do, you know some reasonable assumptions, but it just cannot solve the problem. There's no

information. Then another one is the transmission is the last step in this in this and these are in the India in the indication of what in in both cases is the last step in the risk, and that's that by itself. Even if you had this the first beat of the day. The third one is still a problem that we are going to see now why why and finally is no threat safe range cuz it has had to work around that with with internal locking stuff. Right? So confusing is the last step. Why is that a problem? Because let's suppose that you have a class with

model pricing. Okay that you have to find it in this in the second in the second file and your intention intention in the first file is to load your own pricing of the one the one is Coke to hotel right? That's the one that's intention. But that thing is not loaded, but you have a pricing on the top level low. Because of the Copa that happens if you don't mean that. I haven't even have been technically in in in in in an emote what happens it happens that since Ruby is able to find a pricing constant somewhere.

It is just not going to call Conn's missing. You found something. It it it it it returns done. Right and you get that you get a classroom all your logic that is what it is. Not the one that you expect. So so fundamentally limited is a technique that is fundamentally limited edition technique. Do you know more about the load it dishes dishes the Ruby Outlet thing, right? So you can inactive record for instance dishes done. This is used a lot in the wrinkle bass. You can say I'm going to

Leslie loud bass. Basically, I keep record bass. Okay, so instead of off loading everything you can say whenever you need a cube record bass go and fetch please. 5 The new approach the idea is the following you have you have also Yellow Part, which of the root name is space object. And then let's let's forget about about name is spaces for a moment just understand the technique of europass. Okay. So you said you have a pee pee mother's so when you do stop

you go to a PP mothers and diaper go swimming with us and list the directory if he find you there. It sets an ultra in object to outlawed user and it does that for every entry ended in July in directly and jpiics bigger than the other small things that you can do, but Nevertheless I have tried the idea for years and there was some technical difficulties and it was in in October of last year that there was a new issue about the living in race and I was like, okay, I'm going to work on this. I don't know how I'm going to solve it. I I know that they need I want

to use and I know they have some Roblox, but we will get it. Okay, so technical difficulties. Emoji autoload uses requiring family show visit this does not work does not work well with reloading because require you know, you get if you required. Okay, so there's no Epi to the Mohawk College and if you are reloading and you had an hour to load that had not been executed to make things clean you if you should remove that out. Otherwise you could have been expected clothings in in the next request does not

support name is Pacer so you can say autoload one simple constant name you can I put a qualifier Constantino in Othello. You cannot say in object how to load admin colon colon usage controller. You cannot do that. You need the classroom object instance and set an outlet for that instance. For the next level, okay. And finally, there's a chicken-and-egg problem with a species name is precious. I am going to Spain right now. So. Require is it important

goal of the project to use? To stick with Ruby standard apis into stick with Marisa Monte that's like a goal of the project. So the project needs needs a few Cox, but they are very controlled controlled and they are not like crazy. For instance. This is the first one how does require know if it has to be if if it's already loaded user not in those because there is a a global collection palette loaded features that that remembers everything. So require goes

to the collection and says do I have the use of thing is if it's present then it does not it does nothing and returns false. If it's not surprised if it is not present then goes and requires the file and a store to find name in this loaded features collection sold. The collection is mutable. We just remove the string from the collection on done. So is this this level of cockiness? Then there's no API to remove panel to load but Constance semantically for

Ruby. They are not very different from from regular constant that are already loaded. and it turns out that if you remove const Del Toro this one so short Then name is spaces where we going to wait on you put a qualifier name, right? So this is another little little crack in the case of imprisoning spaces reset a note the Latona directory, which is not documented is not standard yusuke's off Crystal to loading but it works so so we set the alarm clock

scene rapper around cabinet require require until two 3 I believe was not used by Apple or so. When we say how to load uses require until 2 3. It was an internal require different from Colonel required and you could you couldn't access to that require a roadblock for appointment in this scene rapper around and we intercept, you know, the directory we are watching we intercept and if you bought a directory at 2 that require it doesn't it doesn't delegate to the to the active volcano

require intercepts that goes you find the W for you and those who want to step goals has already the Muggle object. Then he can go to that directory or different directory because they Venice Beach going to be speaking different and US1 Livermore. That's that's one interesting observation that driver in the mouth is as lazy as possible. So he's going to do only one level of death time and it is not going to descendants of The Reckless unless you use them, right? Then the chicken-and-egg problem. This was the last difficulty and I

didn't solve these I copied this from a from a gem from Shopify. So the problem is he is you have an explicit name is space like hotel that wants to use one of his mother's then you have the most Define it below write in order to in order to evaluate the first fight you need pricing. But in order to about like pricing Juniper Hotel, so what do you do here? This Mission has been price point so we set the pressure point in the plaza event. This this has to be cheap thing and it shall

be 5 has died back in production. So no problem. This is just an event that that is triggered when classes are created to reopen it as well. Of course, then the thing is that things to the trace point we are call it. So when it's our hotel is is Isabelle waited because you are using that the hotel class song called and then after the class key words before they include line you on your card, so we are on time to go and descend in that at that moment into the

hotel staff directory and put your clothes and then the next line pricing words that wasn't week. So we that everything is solved and the jimmies published. All right, so I'm finalists lights to to splain a little bit just a few ideas about interracial in in in in re6. This is an able by default in re6 new applications or right. He has to be in in NC Ruby Ruby they working on that but for instance still has an internal required for a song by default. If you ain't she wrote song is you have to be in complete application to the repeal of the Ford 6.0 done that. It's all ready

for you. Then I'll be happy to tell you that you can remove for required upon assistant going to be part of history and you don't need this anymore. And if you upgrading arrange application in principle, you should just remove required dependencies on each report. It could be the case that you need to tweak a little bleeding Flector indication of raising Flector. He's active support okay in the case of reporting Flector that comes with diver. It's a trivial thing that.

Like I split, you know, some one liner that is super deterministic. No problem. There's no configuration that is deterministic is under your control. But in raisin than the natural Inflatable Race is at this device and you may need to treat insect or because the classical to louder received the constant name and go to the persistent okay with underscore but but driver goes the other way around. Your first visit Define that the defiled V system and then in first, which should be the constant you find a thing. 5 stats,

lies. That's that's going the other way around. Okay. So these operations are not embarrassed of each other. All right. So capital letters HTML underscore give you a steamer or lower score. But if you go the other way around each other and they may be for some acronym. Maybe you need something but in general should work because we are using the same convention. Grinch already use it but anyway recently implemented this this little check that you can run in your project if you are grading okay, you are great to erase 6

and before 9 below enabling the the the default you can if you want a run this check and he's going through to the deck files that are not defining the constant that are spec then you can go out. Okay, I need an accountant here. Are you going to old to go to sleep later? Or you can even Define your own inflict? Okay, you can do that. Okay, a concentration that is not in the release candidate. It was it was pushed after the release candidate for this is going to be in final is that Internal Affairs a root cellar

rules in the project. One of them is that it is forbidden to do wall pass for me then so there's no need for require dependency and direct only uses absolute Finance internally. So there's no need to have your clothes packed in lopa lopa is the collection that Ruby uses to resolve relative requires right song. No need so maybe I don't know if you were aware aware of that but eight people ATT Motor City controller sleep because they are you know, they are also in the envelope but we

don't need that so we can we can you can opt out by the forties and Abel because it has been there. That way since the beginning so bad. You can talk. So with that you may save some cops in the resolution of relative requires by itself. And if you're using boots not opting out of this saves also boots not work because it does not have to chew go to those. All of the activities does not have to walk your projects tractor and also save memory Snape's memory because it doesn't it doesn't need to build an index for all those

files. Okay to take intercom. At finally you can opt out if you are a grading and you say okay. I have a lot of work to do we are going to deal without reloading later. You can just buy us a coffee cup to the plastic and you are juicing exactly the same ole two others that you were using in your previous version. And finally, there's going to be another thing has and how you feel. Convert this going to be documentation, of course in the operating guide and there's going to be a new constanz guide covering

saving. All right, that's it.

Cackle comments for the website

Buy this talk

Access to the talk “RailsConf 2019 - Zeitwerk: A new code loader by Xavier Noria”
Available
In cart
Free
Free
Free
Free
Free
Free

Access to all the recordings of the event

Get access to all videos “RailsConf 2019”
Available
In cart
Free
Free
Free
Free
Free
Free
Ticket

Interested in topic “IT & Technology”?

You might be interested in videos from this event

September 28, 2018
Moscow
16
167
app store, apps, development, google play, mobile, soft

Similar talks

Penelope Phippen
Developer Advocate at Google
Available
In cart
Free
Free
Free
Free
Free
Free
Sonja Peterson
Senior Software Engineer at ActBlue Technical Services
Available
In cart
Free
Free
Free
Free
Free
Free

Buy this video

Video

Access to the talk “RailsConf 2019 - Zeitwerk: A new code loader by Xavier Noria”
Available
In cart
Free
Free
Free
Free
Free
Free

Conference Cast

With ConferenceCast.tv, you get access to our library of the world's best conference talks.

Conference Cast
575 conferences
23117 speakers
8618 hours of content