Modules and Mixins notes

Make a note while checking

What is a module?

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

How to use

** 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 **

Multiple inheritance

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.

Summary

・ 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 **.

Reference article

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

Recommended Posts

Modules and Mixins notes
credentials.yml.enc and master.key <Notes>
[Java] Basic types and instruction notes
Internal class and lambda study notes
Notes on Java path and Package
Notes on Java's Stream API and SQL
Rails validation and null: false Personal notes