Ruby Module
A Module is a way of grouping together methods, classes, and constants. Modules give you two major benefits.
- Modules provide anamespaceand avoid name conflicts.
- Modules implement amixinmechanism.
A Module defines a namespace, which acts like a sandbox, in which your methods and constants do not conflict with those elsewhere.
Modules are similar to classes, but have the following differences:
- Modules cannot be instantiated
- Modules have no subclasses
- Modules can only be defined by another module
Syntax
Module constants are named like class constants, beginning with an uppercase letter. Method definitions look similar too: module method definitions are similar to class method definitions.
As with class methods, you can call a module method by placing the module name and a dot before the method name, and you can reference a constant using the module name and two colons.
Example
We can define multiple modules with the same function names but different functionality:
Example
Just like class methods, when you define a method in a module, you can specify the module name followed by a dot, and then the method name.
Ruby requireStatement
The require statement is similar to the include statement in C and C++ and the import statement in Java. If a third-party program wants to use any defined module, it can simply use Rubyrequirestatement to load the module file:
Syntax
Syntax
Here, the file extension.rbis not required.
Example
Here, we use$LOAD_PATH << '.'to let Ruby know that it must search the current directory for the referenced file. If you do not want to use $LOAD_PATH, then you can userequire_relativeto reference a file from a relative directory.
Note:Here, the files contain the same function names. So, this would cause code ambiguity when referencing the calling program, but modules avoid this ambiguity, and we can use the module name to call the appropriate function.
Ruby includeStatement
You can embed modules in a class. To embed a module in a class, you can use theincludestatement:
Syntax
If the module is defined in a separate file, then you need to use therequirestatement to reference the file before embedding the module.
Example
Suppose the following module is written in thesupport.rbfile.
Now, you can reference the module in a class as follows:
Example
This will produce the following result:
Sunday You have four weeks in a month You have 52 weeks in a year Sunday 120
Mixins in Ruby
Before reading this section, you need a basic understanding of object-oriented concepts.
When a class can inherit characteristics from more than one parent class, the class is said to exhibit multiple inheritance.
Ruby does not directly support multiple inheritance, but Ruby's Modules have another magical feature. It almost eliminates the need for multiple inheritance, providing amixinmechanism.
Ruby does not actually implement multiple inheritance, but instead adopts a technique called mixin as a substitute. When a module is included in a class definition, the methods in the module are mixed into the class.
Let's look at the following example code to learn more about mixin:
Example
- Module A consists of methods a1 and a2.
- Module B consists of methods b1 and b2.
- Class Sample includes modules A and B.
- Class Sample can access all four methods, namely a1, a2, b1, and b2. Therefore, you can see that class Sample inherits from two modules. You can say that class Sample uses multiple inheritance or mixin 。