English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Module (Module) is a way to combine methods, classes, and constants together. Module (Module) provides you with two major benefits.
Modules provide aNamespaceand avoid name conflicts.
Modules implement mixin Device.
Module (Module) defines a namespace, which is equivalent to a sandbox, where your methods and constants will not conflict with methods and constants elsewhere.
Modules are similar to classes, but there are the following differences:
Modules cannot be instantiated
Modules do not have subclasses
Modules can only be defined by another module
module Identifier statement1 statement2 ........... end
Module constant naming is similar to class constant naming, starting with uppercase letters. Method definitions also look similar: module method definitions are similar to class method definitions.
By class methods, you can place the module name and a point before the class method name to call module methods, and you can use the module name and two colons to reference a constant.
#!/usr/bin/ruby # Defined in the 'trig.rb' file module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. end end
We can define multiple modules with the same function name but different functions:
#!/usr/bin/ruby # Defined in the 'moral.rb' file module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # ... end end
Just like class methods, when you define a method in a module, you can specify a point after the module name, followed by the method name.
require statements similar to C and C++ include statements in Java, and import statements. If a third-party program wants to use any defined modules, it can simply use Ruby require statement to load module files:
require filename
Here, the file extension .rb is not necessary.
$LOAD_PATH << '.' require 'trig.rb' require 'moral' y = Trig.sin(Trig::PI/4) wrongdoing = Moral.sin(Moral::VERY_BAD)
Here, we use $LOAD_PATH << '.' Let Ruby know that it must search for referenced files in the current directory. If you do not want to use $LOAD_PATH, you can use require_relative to reference a file from a relative directory.
Note:In this case, the file contains the same function names. So, this may cause code ambiguity when calling the reference program, but modules avoid this code ambiguity, and we can use the module's name to call the appropriate functions.
You can embed modules in a class. To embed modules in a class, you can use the include statement:
include modulename
If the module is defined in a separate file, then the require statement to reference the file.
Assume that the following module is written support.rb in the file.
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, you can reference the module in a class as shown below:
#!/usr/bin/ruby $LOAD_PATH << '.' 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 result:
Sunday You have four weeks in a month You have 52 weeks in a year Sunday 120
Before reading this section, you need to have a preliminary understanding of the concept of object-oriented programming.
A class is said to have multiple inheritance when it can inherit characteristics from multiple parent classes.
Ruby does not directly support multiple inheritance, but Ruby's modules (Module) have another magical feature. It almost eliminates the need for multiple inheritance, providing a feature called mixin device.
Ruby does not truly implement a multiple inheritance mechanism, but instead uses mixin technology as a substitute. Including a module into a class definition allows the methods of the module to be mixed into the class.
Let's take a look at the following example code to understand mixin in depth:
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 is composed of method a1 and a2 Composition.
Module B is composed of method b1 and b2 Composition.
The class Sample includes modules A and B.
The class Sample can access all four methods, namely a1,a2,b1 and b2.
Therefore, you can see that the class Sample inherits two modules, and you can say that the class Sample uses multiple inheritance or mixin .