Make a note while checking
The basic concept of Ruby is that there are ** classes ** and ** objects ** A set of behaviors in one place is ** Module **
But unlike classes, you can't create objects
The module is just ** A series of behaviors (a collection of methods) ** or ** a collection of processes **
Load it in a class and use it
The basic form is almost the same as the class It is possible to declare constants and classes as well as methods
** Include ** (** include **) in the class you want to use Then you can load the method from the created object
Methods with the same behavior in different classes Easy to add
Do not collide even if you give the same name Each world is called a ** namespace ** The same name may be used in different namespaces
Mixin Incorporating a module into a class is called ** Mixin **
Ruby is a language that does not allow multiple inheritance like Java etc. That is, two ** parent classes cannot be inherited at the same time **
Therefore, After inheriting one class The only way to make changes is to ** modify the child class **
However Mix-ins allow you to behave like a parent class
Also, once you define a module, you can use it in other classes, which is very convenient.
・ Class → object can be created ・ Module → Object cannot be created -A module is a kind of ** method collection ** Can be used by loading in ** various classes ** -It is also possible to load ** multiple ** modules and use ** -Incorporating a module into a class is called ** Mixin ** ・ By mixing, you can make it look like ** multiple inheritance **.
Field Rails https://style.potepan.com/articles/7667.html
Articles that I would like to refer to when checking the operation https://qiita.com/tanaka-t/items/32a3329fbc1eb0840ea0 https://qiita.com/pink_bangbi/items/2c2f17516cd3a7b4eeac https://medium.com/@jiraffestaff/ruby-mixin%E3%81%AE%E4%BD%BF%E3%81%84%E3%81%A9%E3%81%93%E3%82%8D-%E8%A8%AD%E8%A8%88%E7%9A%84%E3%81%AA%E8%80%83%E3%81%88%E6%96%B9-9b8d2e529669