 a'r cyfnod yn y bwysig i'r cyfnod. Yn oed, Mae'r cyfnod yn hynny. Mae'n cyfeirionedd. Mae'n gwybod i ymddydd ysgol. Mae'r cyfnod yn cyfnod. Mae'r cyfnod yn cyfnod. Mae'n cyfnod yn ymddydd, mae ydych chi'n cyfeirionedd yn ysgol a'r cyfnod yn cyfnod yw'n cyfnod ac mae'r cyfnod yn cyfnod i'r cyfnod yn ysgol y pwysig i'r cyfrannu. I wanted to help her to stop to it. But I decided that I couldn't do this job under my normal name. I needed to protect my identity. And so I chose D3B, D for sure. Why? Well, for the sole purpose of making this trick. And today I'm going to tell you about one of my memorable cases. The case of using method. Chapter one. So I'm sitting alone, bored in my office, like that. When the doorbell rings. And it's Mike. And he didn't look too happy. Let me tell you a bit more about Mike. So he was the acquaintance of mine. We had some mutual friends. So we shall be out and about now and again. He was 24 years old, a junior Ruby developer. And he got excited about test-driven development. Now he'd been applying for an apprenticeship at the prestigious Ruby Institute of Professionals. And he'd managed to get through all of the stages, being hunts of applicants, and he was at the final stage and this was happening in two days. And there were two people that were fighting for this last spot. Himself, and will be named Jenny. So Jenny was 27 years old. Also a junior Ruby developer. And she loved all things Rails. She's also Mike's best friend and housemate. And they had quit their consulting jobs at the same time to enter into the world of tech. And not long after they also decided to live together. Now for the interview, they agreed to work together to prepare so that they each had the best shot. May the best person win, they said. Now they'd been asked to research a series of topics that they were going to be grilled on. And one of them was Method Lookup. And this had been assigned to Jenny. And this was the reason that Mike had come to me. Something doesn't add up in Jenny's notes, he said. But Jenny's been so stressed and pallid, rather uncharacteristically, by this interview that she just won't hang me out. She's convinced that she's right and she says that they don't have time for Mike's doubts. As a Mike tells me, that if I can help him find the answer to this mystery, he'll have what he needs to confidently correct Jenny's notes and save them from interview failure. So he reaches into the satchel and draws out some sheets of paper and then Jenny's supposedly flawed notes. I asked him to walk me through them. So she's got these boxes to represent the concept of a ruby object. And all of the objects have a label called class. This acts as a reference to the parent class of the object in question. So some objects are instance objects. And the class label for them are the other ruby object of type class. An object of type class has its own class label. And they also have a method label. And this points to a table of all the methods that instances of that class can call. And then Jenny's written, any class you define is an instance of a class object called class. So if we were to write class cake in our code, we're creating a class object name cake. And it's an instance of another class object. And that object is class. So all classes are of type class with capital C. And then Jenny had this code in her notes. So it was class cake and there were two methods. One was an instance method called tasty, which returned true if the flavour of the cake was carrot. So I immediately knew that Jenny was a smart woman. And then there was a class method called edible which always returned true. And then she wrote, imagine if we had a cake instance called carrot. Then this is what the method look of chain would look like. We'd have our Ruby object carrot. It would have its class label. This would point to another Ruby object of class cake. It would also have a class label. And there would be a methods label, which would have a table. And in fact that table would be tasty because all those instances of cake could call tasty. And then the class label for cake would point to a Ruby object called class. It would have a methods label and that would point to a table with the entry edible. And then Jenny had written, a method definition always comes from an object's class. And at this point, Mike shook his head with frustration. It cannot be that the edible method lives on the parent class of all classes, he said. Can we show you something? Can I jump in your computer? I said, okay. So he goes over, opens up terminal, enters prime and he loads in the cake class from Jenny's notes. So the first thing he does is to prove that the class for cake is indeed a class. And then he checks the instance methods for edible. Nothing. What a mystery. So I'm stumped. I'm not sure how to proceed. But D loves the challenge. And if anyone can solve this, then D can. And besides, this meant a lot to Mike and he was prepared to pay me handsomly. So I agreed to take on the case. Chapter two. Ever heard of Google? Or if you care about your privacy and people like me not spying on you, perhaps you used up, up, go. Well, when I'm D, I don't believe in these tools. I don't trust that. And it's no coincidence that with this approach I've become the best movie PI that the industry has to offer. And so what did I have? Books, books, books and more books. And I spent the whole day quickly skipping a lot of books. I couldn't find anything useful. So I thought, okay, let me form a hypothesis and go from there. I said that the edible method, while not in Hague, must be somewhere in the ancestry tree. So what's the ancestry tree? Well, it shows all of the classes and modules that a class inherits from. So all the possible places are method and protocol. So I built up this where method and it took two parameters, an object and a method. And so what it did was it looked through the ancestors of an object's class in order to find a class where the instance method of that class and only that class included the method that I was looking for. And so at first things first, I thought let me check that this method works. So I created an instance of cake and I checked that I could find a tasty method in relation to it. So it worked, cake. And that was the moment of truth. Where was the edible method hiding? It had to be somewhere. What? Nowhere? What a mystery. And so at this point I'm confused and I think, okay, it's time for some fresh air and change of scenery. So I decided to head to my favourite co-working space. And here I feel like children once surrounded by people hacking away and I quickly settled down upon deaths. But given my natural inquisitive nature, my eyes couldn't help but straight the screen of the guy next to me. Now he was an IRB who was playing something called object space. I said, what's that? Looks interesting. He said, well, it's a way that you can interact with all of the live objects in the Ruby session. For example, if you were to type this into your terminal, then you would count all the live class objects that you have in the Ruby session. I thought, okay, I've been working for a long time now. It's probably time to have a break. So I go into IRB and I count up all the class objects. 936, so I think, let me create a class and see that that number goes up by one. 938? That's strange. Let me try again. 140. What a mystery. But I also went to Norfolkia by it because the phone rings and it's my friend wondering where I was. See, I completely forgotten that I agreed to go to a tech lunch with her. And I was anxious that I didn't have much time left to solve Mike's case. But I've also been counselling on this friend a lot lately, putting my PI duties first. And so I thought, on this occasion, I should make some time for her. And so I rushed out of the door. Chapter three. I turned up just in time to the lecture. And it was about a language called small talk. I wasn't that interested. I only had space in my heart for Ruby. But you know I was there for my friend. And the lecture was talking about how small talk was born in 1970 and it had led to the creation of the object-oriented programming pad. But I couldn't concentrate for much longer. I couldn't stop thinking about my play with object space. Each time I created one class, but yet there were two new objects being created. Psst! Pay attention, said my friend. So I looked up and I saw the lecture asking, well, what is the class of a class in small talk? And she had this colour on the screen and I had it in the following. But she had this polygon object that she printed out and she asked the system what is the class of this polygon object in a returned polygon class. And then she said, well, what is the class of this polygon class? And it returned metaclass. The class of class is called metaclass, the lecture reiterated. And she wanted to say that all languages that small talk has inspired have their own concept of metaclasses. And this included Objective C, Java, Python, and Ruby. Something click, one class, two objects. And so I've been going to my students to be known as a friend and rushed out of the door home. So first things first, I thought, let me try my luck. But of course that would be too easy. So I said, well, let me look at all the methods on here. How is this rather overwhelming? So I thought, okay, why don't I filter this down to all of the methods that include the word class? Okay, so this was a lot more manageable. And two methods stood out for me. One of them was superclass and the other one was singleton class. Next step, let me remind myself of the answers to each of the cake. Cos I knew that the other method didn't live in any of these places. Now it was time to see what cake superclass returned. So I knew it wasn't the right answer cos Object was in the case class answer usually. And this was a subset of that. So now let's see what singleton class gave me. Well, this was different. And when I looked at the Ancestry Tree for cakes that was in class, I saw three new objects that I hadn't seen before. Very interesting. And so I thought, let me go back to my way method. And instead of looking at Object's classes ancestors, let me change it so that I look at the ancestors of an Object's singleton class. So I could search through the new classes that I just discovered. And then it was time to give it a go. Result. And I thought, wait, let me just check that what I think is happening is actually happening in my excitement. I forgot how to type. But I got there eventually. I found that the other method was de-living on cake singleton class. Case closed. Chapter four. So at this point I'm delighted and I'm super excited to share this with you with Mike. I also took a moment to wonder can you actually retire now? This would prove to be one of the biggest successes of my career. And they always say that you should quit while you're ahead. Anyway. So I get in my football and I say, hey, can I come round? I solved the case. He's excited. He says, of course, come round and I'll have to know what's ready and waiting for you. But when I solved the case for Mike I wasn't satisfied. I just discovered a whole new concept in the language that I hadn't heard of before. What are singleton classes? So instead of going directly to Mike I decided to take a detour to visit a friend of mine. Her name is Ellen. She is 43. A free non-celebrity she's regularly contributed to the Ruby language code base. So I received to tell Ellen all about the case. But what I've done, I asked her so what are these singleton classes all about? And she says, well, they're hidden classes created internally behind the scenes in Ruby and they're there to hold methods to find only for one particular object. So take your instance carrot of the cake class. In singleton class we hold methods specific only to carrot and not to another instance. Say if you have one called red velvet or chocolate. Okay, I said. So given their work and their way behind the scenes when does doing about them become useful? So Ellen thought for a while and she told me about one of her recent clients. And they were going to do ink. And they were creating a clever artificial intelligence that was going to help a clever artificial intelligence machine learning personal finance tool for small business owners. And they were expanding globally and needed to roll out slightly unique versions of their software for each new city that they entered. So Ellen told me that when she first looked at the code base she was horrified. Different developers have been responsible for each new city. And I noticed if they were trying out a new approach for naming things, testing things, designing interfaces so there was a lot of duplication. And there was also therefore lots of ways to tackle development because these developers were often reinventing the wheel and doing a bad job. I said a lot of bugs. You know, some things had been copied some things had been left out and it was just difficult to see what was important. The developers were unhappy because they had a lot of time to work and the cognitive load was high. And the product owner was also unhappy because delivery was either very soon or things were spun out quickly and then were therefore bug-ready. I wasn't quite sure what to do and then she said to create a deer cell a domain-specific language and she asked me if I knew what she was talking about. It's a mystery to me, Ellen. You're going to have to explain. So she said, let me show you a basic version so she called me over to her computer and she opened up a price session and input this class so it was called City Instance and it had a class method called Construct which took a block. Then we had a variable called City which called up to initialise by the new keyword. Then we called instance of our own city passing in a block and Ellen said I should pay attention to this line because it's going to prove to be important later. Then we returned to City. We had an attribute called Taxes and initialised method which has an instance variable called Taxes as an empty array and then we had a method called Tax which took a name argument and that added that tax to the Taxes instance variable. Now it's time to get it to go. So Ellen created a city called New York and she added a couple of Taxes and then she bridged out New York's Taxes and then we have it to Ellen. That is a very simple DSL and we can quickly spin up these line-up with city objects. Imagine if we had other properties she said like a list of balance or finance schemes and imagine if we had more information about each of these properties. So maybe the Taxes also had information about rates of thresholds and so using this simple framework it's not hard to imagine extending the city instance class to be able to create incrementally more complex city objects. What imagine taking that to the next levels of Ellen? Imagine interacting with the city instance class in the same way on the command line but instead of just creating variables in our prior session we were spinning up new sub-classes of each city and other related models for each tax scheme bank and then Ellen explained to me that this was the sort of thing that she produced for Budgeting Inc, a DSL that allowed for quick and easy scaffolding of each new city sub-class and any related classes. Okay, she said, so now we know how to spin up identical city instances with different names and different taxes. But what about if one city hadn't worked? So, since I was from the UK she said, why don't we think about how this would be UK? So, I suggested Bob since that was the scene of my first ever case. So, she said, okay, so she created a city for Bob. She had an attack. And then she asked me to think of something that made people in the UK really unhappy. If you didn't like my suggestion she thought it was a 2.0 virtual so she thought, well, let's say it was focusing on the fact that it raised all the time and when it does the government clears everybody's taxes because they feel sorry for everyone. So, we have Bob. We live in a taxes and then it rains and so the government calls a rainy day amnesty. The taxes again, there's nothing there. Now, remember our friends in New York, said Ellen? Well, they've heard about this rainy day amnesty and they want their own one. But as of the next time it rains they try and call one but they get an undefined message. So, city instances and then there are all these characters and Ellen asks me, but do you know what these characters are or what this object is? I said no, it's a simple look at this. And she calls simpleton class on New York and I saw that the objects were the same. So, when we enter the realm of DSLs and we're calling methods like instance avow what we're doing is we're leveraging the existence of simpleton classes but this instance avow stores any method declarations passed in via the block onto an object, simpleton class. But hold on to that thought, Ellen. I want to take a step back to the high level for a moment. So, she said that by creating a DSL like this for a client it enabled the developers to spin on each new city instance effortlessly. So, she explains how she managed to abstract out all of the key similarity between any city. So, now her benefit is her frictionless way via the command line spinning up the foundation that they needed. As of now the code base is better maintained because all of that scaffolding had been well tested. Ellen had dealt with it once and none of the other developers now had to worry about it. In fact, they could now just focus on the interesting bits which was the customization required for each new city. So, the scope that they were dealing with was much more refined. So, now the developers are happy because interacting with this system was a joy it was easy to have a high level overview of the whole domain in their head purely by the documentation for the DSL. And the product winner was delighted also because there were far fewer bugs, faster delivery and she could also speak in the same language that the developers were using by expressing new requirements by the terminology of this DSL. But, let's go back to Stilton class incident. So, why is the learning about them useful? Well, she tells me that one of the main reasons that she was able to defeat the project to a high standard was because she understood exactly where she was defining methods at any given point because once you enter the realm of dynamite-creating classes and methods on singletons as you saw in your investigations the class hierarchy and method look gets far more interesting. So, you might be getting error messages and you need to be able to spot where singleton methods are involved and where they're hiding as it could save you from a lot of headache but be where to end. I've been going on and on about DSLs but they're not the answer to everything. So, perhaps you have a case where you've got complex repeated business rules and maybe you need to customise behaviour in some specific instances then, yes, you can consider DSLs as an option. But even then, approach with proportion. But, and he said, you don't even need to be writing DSLs to be beneficial for you to understand the basics of how they work. So, she said, if you're using Rails you see DSLs every single day. She went over to a computer, went over to the Rails guide website and said, well, when you're doing your active rebel migrations and you're specifying your table names and how you'll perform out of a column those are carrying out via DSL. And when you're specifying how your Rails app handles HTTP requests that's also done via DSL. And I had just been doing this stuff by road. I never stopped to think about what is actually happening behind the scenes every time I type the resources keyword into my conflict with our B5. And there's more of these at edge. So, when people talk about Rails magic she said, it's not really magic. It's more of a collection of Rails written DSLs. And then Anna looked to me and she said, I hope you're TDDing all the time. And I said, of course. What do you take for? And she said, well, what aspect with its described context of it not those are all DSLs too. And so, with all of these Rails and aspect DSLs, when you know about Singleton classes it can really help. Because you might find yourself in a tricky problem. You can't make it particularly if you inherit a code bit. And so, if you're seeing a funny bug to do with methods you never know Singleton classes might be the answer. And having them as part of your sweet or debugging questions can be useful. So, I was feeling really leveled up by the end of this conversation. And armed with this new knowledge I headed over to Mike's. But when I arrived I found that Mike had tears in his eyes. He'd obviously been recently crying. And he had crumpled pieces of paper in his hand. And he raised his arm towards me offering me these papers. And they looked exactly the same as the method look at notes of Jenny is that he brought to me the day before. So I took the notes from him and I looked through them. I couldn't see what was wrong. They looked exactly the same. So we had the carrot object with its class label and that pointed to a cake object with its class label. With the table with Tasty in it. And then we had the class object with its method label and the edible entry. But wait, it didn't say class like last time. This time it said cake Singleton. And as Mike told me notice this difference he fell to his knees and broke down in tears. Jenny knew about Singleton classes all along. He'd gone into her room to find the notes in advance of my arrival and this was the copy that he found. Turns out that Jenny was so desperate to secure the job for herself that she had set out to intentionally misfeed Mike in the hope that he would fail a whole section of the interview and therefore the company prepared him. He pointed in my stuff. See I was so focused on the main villain the shady mastermind that I failed to spot a villain right under my nose. My best friend tried to sabotage me Mike cried. At this point he started wailing saying he wasn't going to go into the RIP interview tomorrow and I said nonsense you can't give up now. So I crouched on next to him and gave him a comforting pat on the shoulder and I said you can do this and I have just the thing to help set you apart from Jenny and he looked up at me hopeful. Have you heard of dear self? I asked him. And I didn't tell him everything that Ellen had just shared with me. And although Mike still looked devastated as I left him I have confidence that I have inspired him with the power of singleton classes that he put himself together and go and secure that RIP internship for himself. So it's two months later and I managed to drag myself to a really happy night. And I'm meaning about enjoying the free food and drink when I hear a couple of people whispering in the corner ooh she's going to fail us one of them said and I look across the room and I see Ellen so I walk over and we cat up and I tell her that I've been reflecting on the case of the missing method of my takeaways that there are singleton classes that are there to hold methods to fight for one particular object. And I mean you understand them it opens up a whole new world through B when you're dynamically creating classes and methods and you're using more complex applications for things like dear self. But I said to Ellen I still feel as if day to day I'm going to get by ignoring them. So do I really have to care about them at all? Well not really, said Ellen like you say if you're using dear self then yes you definitely should understand how they work. And they do want to prepare popular frameworks like rails but day to day you can get by however she says understanding why singleton classes are there I think is super interesting and empowering. Think of the Ruby quality they want to keep things as straightforward as possible. By simple that means minimise special cases aim for having one way one pattern for explaining how anything works. So that they continue asking how consistent can we get things and what's consistent in the realm of Ruby methods? Well all methods in Ruby are defined in only one of two places a normal class object or a singleton class and every method in Ruby is really an instance method and method lookup always starts with a singleton class. So what we call class methods don't really have a fundamentally different behaviour what they really are instance methods where the objecting question is a class and the method is stored on the class of singleton. So yes singleton classes are invisible but they are everywhere they are a fundamental part to how Ruby and its method lookup works. So I left the hack night deep in thought and it had inspired me to explore more of Ruby behind the scenes because there was so much here that I didn't know and I'm going to ask for service and I've always been a general Ruby pilot at Stata and I've been very accessible in the field but perhaps it was time to find a niche and reach the next level.