 And this title is a quick bait. I'm not going to talk about the simplest gem you'll ever use. I will be talking about the gem that I created some time ago. And it is pretty short, probably not the shortest one. So recently I was checking what are the actually simplest gem that I'm using around and I found a few that are, I found plenty of gems that are below 1000 lights of code. Surprisingly, most of them were related to JSON, I don't know why. So one of them is active support JSON encoder, which fixes some issues that appeared in Rails after they changed the library that was encoding JSON. So this gem in 200 lights of code provides you some functionality. Basically they fix what they broke. Then you get another one, which is Rails pass, patch JSON encode, which fixes JSON in Rails as well, but it fixes the performance. So this gem provides you some value in 35 lights of code. And then the last one, the really simplest gem that I found, and this is the simplest gem that I'm using, is called all-JMIMI JSON. And it provides you some value in 10 lights of code. However, this gem, the only thing that it does is that it replace the JSON library that you use with the optimized JSON, which is written in C and makes it way faster. So you don't need to change anything in your application. You just load this gem and it replaces the library. So 10 lights of code and you get a much better JSON speed. So the gem that I wrote is called SimpleOperation, and it's a version that I wrote 10 months ago who had 33 lines. And last night, I added two new features, and now it's crazy. 40 lines of code, 42 lines of code. So it is not the simplest gem that you can ever use, but it's pretty close. This gem, as its name suggests, it implements operations. So what I call operations is sometimes called actions or services, and these are basically classes that allow you to perform some set of actions. So as I see it, I treat this operation as kind of a function. So it has inputs and it has output. I provide parameters and I get the result. So I build a class that behaves like a standard function, and I do it because it might be a bit more complicated than a normal function. I want to have some private methods in that class. So that's why I use a separate class to perform a single operation. This class has one public method, except for 50. It has because it's Ruby class and every Ruby class has 50 public methods. But I'm only just one public method, and its name is co, and I will explain you later why. I name these classes with verbs, so I don't call this class user creator, or I don't call this class checkout, I just call it create user or make a booking. And what I try to do with these classes is to prevent it from keeping any state. So you use it as a function. It doesn't hold anything inside. You have input, output, and whatever happens between is not available later. So I will show you an example of what I do. An example is that I want to select valid records from an array of records that I provide. So I create a new class that is called select valid records. It inherits from my class simpleoperation.new. And simpleoperation.new with some parameter will return you a class. So this is, if you use struct class in Ruby, you will find it very similar. Then I define the method call, the only public method at 95. And whatever happens next is just private. It's just for the purpose of running this one method call. And this is the whole class. This class behaves like a function. And I call it this way, I validate records dot and then I provide parameters in parenthesis. So this syntax might seem quite weird to you because it is not very common. First, because normally when you create an object you do class.new. And then because here after a dot you've got parenthesis right away. You don't have the name of the function. So validate records, this class simple operation defines a call method on a class level. That's why when you do this you actually, what you call is validate records dot call. And Ruby has this nice native syntax sugar that when you have method called call you don't need to provide its name, you just do dot and then you use parenthesis. What's interesting is that this class behaves exactly the same way as croc or lambda. So I find it interesting because I can provide either lambda, I can provide block of code or I can provide this class and it behaves exactly the same way. You just put dot call and it calls it as a function. Now another feature that I have is that sometimes you need to return something more advanced than just a simple value. So usually in such case you can use multiple return, so you can like return 1, 2, 3. But I don't find it really convenient because then the order really matters. So you need to remember what is the order of arguments, order of results that this function gives you. So that's why in simple operation on the class level you have method called result where you define what you will later return. And then in my method call instead of giving return in the end I call result and I put their values that I want to return. So now instead of having just a simple array as a result of this function you've got a struct that has named fields. So it's much more convenient than having multiple values returned in specific order. Now let me show you the code of this gem. So the whole gem consists except for all these gem spec license, etc. It has just three files, one of them is version, so it does almost nothing. One file is extension that I just added for fun and I will explain in a moment. And now this is the whole gem. This is everything, this is its whole body. Let me just make it bigger. Is it enough for everyone to read in the back? It doesn't become bigger than one. Now, can you read it now or still pull? It will go larger. It will go larger. Now? Good. Okay cool. So this is just like 37 lines of code here. So there are a few Ruby tricks that I used in this gem and I wanted to share with you. Some of them I already presented a few months ago on the five random Ruby tips. So first thing here is what I do in a simple operation is that I override the method self.new. What it does is that when you call simple operation.new it doesn't actually return simple operation. It returns something different. So for example, let me show you here. For example, if you hate your coworkers, you can do something like this. class hash that self.new return an array. And now if I do hash.new, it returns an array. And then you will see them banging on the wall. So this is the way that struct also works. And this is the way that my gem simple operation works. So it returns you a different class than the simple operation. Now what I do is that what I return is the class.new. So I return class object. So this is alternative syntax for creating class in Ruby. So you got the class and then the name and this one. This one is way slower. It's like five times slower. But way you don't define class on every call of your function. You just do it usually on the start of your application. So this speed doesn't really matter. But what it does is that you can define the class inside some method, inside the body of a method. You can't do it with the native class syntax unless you use the eval method. So now I'm defining the class. This class takes first, it evaluates the block. So whatever I do in simple operation inside the class will be evaluated another new class. Then class eval also can take string as argument. So I couldn't provide this code as a native code. I had to use the string because as you see I have the list of arguments with nils. So it's a string so I just embed this string here. And whatever I put here is later interpreted as Ruby code. The next thing is perform method which I added here just for the purpose of compatibility with something like delay job. So I often use this in the background and just because I didn't want to write this method perform myself all the time I just added here. And then you've got a few private methods. So first is all add reader with the arguments. I added it as a private because I don't want people to use this from the outside of the class. This class is supposed to work as a function. The only way to reach it should be via the call method. And then this thing result. So first result is the class method we sell. And it basically defines the structure. You write the arguments there and it creates a new struct, a new class that will be representing later the result that you return from the function. And the second one is with the same name but it's an instance method. It's not a class method anymore. It basically finds this result class that I defined above and it returns instance of it. I did it via this instance variable get. And this is because I didn't want to make this public once again. And I didn't want to write any method that returns it outside of it. So I just used instance variable get. And that's actually all the code that is here. The only thing left is the extension file which overrides which adds the new method to kernel module. So kernel module is basically everywhere in any of your Ruby applications. You've got included kernel module. So now you can call something like... So now I can do something like simple operation new. And it works. And if I require the extension, what I get is also that I can use it as a method. So I just call simple operation. So it's both names of the class and it's the method. And the same works with array. So you can do array.new. Yeah. And then you can do array. And it works in a different way but still works. Okay. So that was the code. So the tricks that I used here is that I'm overriding the class new. I'm using class evo which is a metaprogramming technique. I'm using class method as a method name in the extension that I showed you. Assigning class to a constant. I didn't show you this one. So you can do something like... If you do class A, let's say class A. I'm going to find something. You can discuss. So this is one way of defining it. But now what I can do is that I can create a new class. You will inherit from A. So I'm just basically assigning the result of the class new method to a constant. It becomes a new class then. So this is another thing that... I think it's a very neat but not very popular Ruby syntax. And the last thing that I do is that I use dot and then parenthesis instead of calling the call method with its name. If you're interested in it, the gem is on GitHub. I've been using it for, I don't know, almost a year. And I use it in all of my applications just because this pattern that I'm using is so common. It is somehow related to train laser library. If you've heard about it, it's like an architecture on top of Ruby on Rails. Nick has this whole set of Ruby gems that he uses to facilitate his work with Ruby on Rails. And one of his gems is called operation. It's somehow similar but a bit more complicated than the concept that I'm using. I think that's it. If you're interested in such stuff or you're hiring at Caligo, we're hiring Ruby and RLang and Elixir developers. And if you have any questions now, don't hesitate to ask. No questions? Okay, go. Thank you very much.