 Okay, can you read it without problems? Is it big enough? Okay, cool. I can't use microphone because I will be coding Because then I can look at the screen. I'm not so stressed So my name is Gregor. Three months ago at similar Ruby Meetup I was presenting five not so random Ruby tips because they were all related to animators. Today my topic is creating objects As it seems not to be so interesting wait a few minutes and you will see that is More complex than than you can imagine So Let's start with something simple hash hash is one of the most Commonly used class in Ruby language So the basic way we create hash is like this then we've got empty key value store, but we can populate it With some data since the beginning We can use not only symbols, but also for example class or We can use another hash As a key in our hash we can use basically any object in Ruby as a key in hash But there are many other ways to creating to create hashes for example I can use hash.new and Then I can provide some value for example zero This hash is empty But if you check for example any random value it has zero because I provided it as a as a parameter to hash new So it's the default value if now I override it Then my hash Consists of this element But any other element is once again zero this was I Think Michael one or two months ago talk about it a bit deeper so we can check the video on YouTube I will show you another way of creating hash, which is using square brackets. So let's say I want to Hush, and then I will put a one e2 and now it uses odd elements as a keys and even elements as As values now this is not very This is not very comfortable way of creating hash But for example, if I say if I have two arrays Sorry So I've got two arrays and now I want to have an a hash where ABC will be keys and one two three will be values So I can iterate over array and I can create hash out of this or I can do something like every two zip everyone Then I've got then I've got an array We were where I have already grouped elements now I can do something like Flatten and now this is already one array now If I use hash and then square brackets, and I will use splash operator Ash and I use splash operator it creates a hash of two arrays in one simple line of code Now this method It seems quite magic syntax, but this is not nearly as magic as you can imagine Let's say that I've got my own class class my hash and I want to I want to define The same syntax. It's very easy. I just define self dot square brackets And now I can read my hash One two three four and it returns It returns array in this example So that was tip number one creating hashes number two creating arrays Arrays once again our very simple objects And very commonly used. So this is the basic way we create arrays Now if you want to create arrays of strings, we usually do something like a The C in single or double quotes, then we've got array if we don't want to type quotes There's much simple way, which is something like this And we've got the same result if we replace parentheses with curly brackets or square brackets or something else It will still work and if if we replace letter w with I we have symbols instead of strings Now there is there are another ways of creating arrays in Ruby. So we've got array New and it takes two parameters first parameter is the size of an array and the second parameter is the default value Now there is another yet another way of creating an array, which is something like this Array and then parentheses. So this turns me into into empty array It will turn number one into array consisting of number one and it will take an array Once again into an array. This is very useful if you don't know what parameter comes to your method If it's me and if it's number if it's string if it's whatever and you need an array You just do it this way. Now how it happens that something which is Which is not a method which is a class can can can work as a method as it appears in Ruby You can create methods that start with capital letter. So let's say I define my method my hash and now my hash Seconds then if I provide here one two three Then our then I will have an an array of results. So the same happens with array Array itself. This is a class. But if you put parentheses, it functions as a method So Ruby basically first checks the constant if it if it exists in the table of constants If it doesn't then it checks if it's a method Now that was number two. This was creating arrays. Now. Let's create some classes As you know the basic definition of creating classes something like this class my error This is very basic way and this is the way that almost everyone creates classes, but there is another way which is class dot new course class itself Has its class Which is once again class. So in Ruby class is a normal object. That's why it has initializers So we can use method new and it takes one parameter, which is Which is parent class. So now I can raise this. This is anonymous class. It doesn't have a name But since classes names are constants Now under the name my new error. I have this new class and Just to show you that is Yes. Oh, no, it's not runtime error Okay, anyway, it shows It shows the message it behaves exactly like runtime error So this is this is the way of creating classes and Just so that it's not only class new But also you can provide something more useful here Now I got a warning because I redefine my class, but I can run my error new Show me Huh, I screwed something up Excuse me. Ah, yes. Thank you Okay, and it works. So this way of defining classes is about, I don't know, like probably 10 times slower But this is very useful if you want to create anonymous classes The thing with normal class definition is that if I have my method And here I want to define class Ruby says that I cannot define class in a method, but I can do the same with creating class new So if you ever have a case where you need to define class in a method You can do it only only this way Okay, down to number four. Number four is a trick that I learned recently if you have a class called struct and You call struct new and you say let's say this will be a user class user has login and password Struct new returns anonymous class which itself is not a struct. So As you see user class is not struct class. So struct that new doesn't return a struct It returns something else and it was bothering me. How is it possible? Because if I define my class and I have initializer and I want to return user instead of new user It's still It still defines it still returns new user. It doesn't return user. It doesn't return what I want So what I realized is that initialized method is an is an instant method It is called on an object that is already created and what I need to do is I need to overwrite The method that that is responsible for creating object. So how do I do it? And as you see now new user that new returns my my struct which is user that consists of login and password so if you have There are some ideas that you can use this trick for it's not very popular But struct I think it's the best example. I use this class very very often and and I want I wanted to know how it works Now number five cloning objects. So cloning objects creates new object. Let's say I have object is object.new and If I type object clone I have as you see it's a different it has different object ID than the previous one Another method of another possibility is to object.dap which creates yet another object and people very often confuse this method They think that they are exactly the same that there are aliases, but there is one simple thing That differs these two methods. Let's say that I have my object and I have module a So now I have my object and I extend it with module a So It is so my object is now extended with the module a Now if I top my object It appears that it drops the module that it extends that it that it was extended with and If I use clone it keeps them So if you sometimes extend your objects and if you clone them Just remember that these methods are not are not exactly the same they are a bit different and These were the five tips, but I've got something else something that I think it's quite interesting Since we realized that class is a is an object and that we can create new class and We can provide some parent class what happens If I want to inherit class from a class anyone has an idea what will happen No, my computer will not burn object will not be created Ruby has Has a special message. It says that you cannot make subclass of class This made me a bit sad because I wanted to play with this a bit more Thank you very much for listening first track So I wrote this simple library that is called simple operation and this operation class actually wraps around Like it behaves like a struct I provide a Class that takes parameters and it has just one method So this class has similar interface to a method, but it allows me to use instance Instance variables and similar stuff inside So I just called simple operation dot new then I provide parameters and these parameters inside my class are Turned into private readers and I have access to them inside inside this class So there is a gem you can see on my github. It's called simple underscore operation and I use some of this tab that I showed here No, any more questions Okay, thanks Let me get you set up