Module in Ruby

xiaoxiao2021-03-06  108

1. Differences between class:

a. Can't have an example

b. Do not have a subclass

c. Definitions by Module ... END.

2. There are two main effects:

a. As a method or a constant aggregate, such as MATH modules.

Irb (main): 001: 0> Math.sqrt (4) => 2.0irb (main): 002: 0> Math :: pi => 3.14159265358979irb (main): 003: 0>

It should be noted that :: operator is used to reference modules or categories.

b. Used to Mix-in.

We know that there is a single inheritance in OO, such as Java, multiple inheritance, such as C or Python, Ruby is also inherited, but can also achieve multi-inheritance, like Java, can also be achieved by min-in. The so-called MIX-I is introduced into another module or class by incrude constant or method in one module.

ENUMERABLE is an example of Mixin in the standard library. By putting it Mixin to your class, then if your Each method returns an object, you will have functions such as sort, find.

Mixin and multiple succession differences:

The module does not produce an instance module to keep the class level relationship to keep the tree, not a mesh.

转载请注明原文地址:https://www.9cbs.com/read-96283.html

New Post(0)