modularAre methods, classes, and constants that are grouped together. The module has two main benefits:
- The module provides a namespace and avoids name conflicts.
- The module implements a hybrid factory.
The module defines a namespace. Methods and constants in a sandbox can be used freely without worrying about stepping on other methods and constants.
Syntax:
1
2
3
4
5
|
module Identifier statement1 statement2 ........... end |
Like constants in a module named class constants, the first letter is capitalized. The defined methods look similar. Module defined methods are like class methods.
Calling a module method is the same as calling a class method. It refers to a constant before the module name through the module name, and uses the module name and two colons.
example:
We can define a function with the same name but in different function modules:
1
2
3
4
5
6
7
8
9
10
11
|
#!/usr/bin/ruby # Module defined in moral.rb file module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # ... end end |
Like the method of a class, when the method defined in a module is followed by a point after the specified module name, the name of the method will be changed.
RubyRequire statement:
The require statement declares an include statement similar to C / C + + and an import statement of Java. If a third program wants to use any defined module, it can simply use the module file loaded by the ruby require statement:
Syntax:
require filename
It is not necessary here RB file name extension.
For example:
1
2
3
4
5
|
require 'trig.rb' require 'moral' y = Trig.sin(Trig:: PI / 4 ) wrongdoing = Moral.sin(Moral:: VERY_BAD ) |
Important: here, both files contain the same function name. Therefore, this will lead to ambiguity in the code, which is also included in the calling program, but the module avoids the ambiguity of this code, and we can call the name of the appropriate functional module.
Ruby include statement:
Can be embedded in a class module. To embed a module in a class, you can use the include statement in the class:
Syntax:
include modulename
If a module is defined in a separate file, it needs to contain the file, which needs to be hidden in the exposed module before the require statement of a class.
example:
Consider the following modules written in support RB file.
1
2
3
4
5
6
7
8
9
|
module Week FIRST_DAY = "Sunday" def Week.weeks_in_month puts "You have four weeks in a month" end def Week.weeks_in_year puts "You have 52 weeks in a year" end end |
Now, this module can be included in the following categories:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/usr/bin/ruby require "support" class Decade include Week no_of_yrs= 10 def no_of_months puts Week:: FIRST_DAY number= 10 * 12 puts number end end d1=Decade. new puts Week:: FIRST_DAY Week.weeks_in_month Week.weeks_in_year d1.no_of_months |
This will produce the following results:
1
2
3
4
5
|
Sunday You have four weeks in a month You have 52 weeks in a year Sunday 120 |
Mixed types in Ruby:
Before passing this section, it is assumed that there is object-oriented concept and knowledge.
When a class can inherit from multiple parent classes, the class should show multiple inheritance.
Ruby does not directly inherit from supoprt, but Ruby modules have another wonderful use. They almost eliminate the need for multiple inheritance and provide a factory called blending.
Mixed types add function classes to a wonderful control mode. Mix classes in the code, and the code that uses them can interact.
Let’s take a look at the following example code to get an understanding of mixed types:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
module A def a1 end def a2 end end module B def b1 end def b2 end end class Sample include A include B def s1 end end samp=Sample. new samp.a1 samp.a2 samp.b1 samp.b2 samp.s1 |
Module a includes a method, A1 and A2. Module B includes a method, B1 and B2. The class example includes two modules. Samples of classes a and B can access all four methods, namely A1, A2, B1 or B2. Therefore, you can see that this class inherits from two module samples. Therefore, it can be said that the example of a class shows multiple inheritance or blending.