May 12, 2021 Ruby
Module is a way to combine methods, classes, and constants. Modules offer you two main benefits.
Module defines a namespace that is the equivalent of a sandbox in which your methods and constants do not conflict with method constants elsewhere.
Modules are similar to classes, but with one difference:
module Identifier statement1 statement2 ........... end
Module constant naming is similar to class constant naming, starting with capital letters. Method definitions look similar: module method definitions are similar to class method definitions.
Class methods allow you to call module methods by placing a module name and a dot number before the class method name, and you can use the module name and two colons to refer to a constant.
#!/usr/bin/ruby # 定义在 trig.rb 文件中的模块 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 # 定义在 moral.rb 文件中的模块 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 dot number followed by the module name, followed by the method name.
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, you can simply use the Ruby require statement to load the module file:
require filename
Here, the file extension .rb is not required.
$LOAD_PATH << '.' require 'trig.rb' require 'moral' y = Trig.sin(Trig::PI/4) wrongdoing = Moral.sin(Moral::VERY_BAD)
Here, we use $LOAD.PATH and lt;'.' to let Ruby know that referenced files must be searched in the current directory. If you don't $LOAD to use the file, you can require_relative to refer to the file from a relative directory.
Note: Here, the file contains the same function name. So, this causes code blur when referring to the caller, but the module avoids this code blur, and we can use the name of the module to call the appropriate function.
You can embed modules in classes. In order to embed modules in a class, you can use the include statement in the class:
include modulename
If the module is defined in a separate file, it is necessary to refer to the file with the require statement before embedding the module.
Suppose the following module is written in the support.rb 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
You can now reference the module in the class as follows:
#!/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 results in the following:
Sunday You have four weeks in a month You have 52 weeks in a year Sunday 120
Before you read this section, you need to get a first look at object-oriented concepts.
When a class can inherit the attributes of a class from multiple parent classes, the class appears as multiple inheritances.
Ruby doesn't directly support multiple inheritance, but Ruby's module has another magical feature. It virtually eliminates the need for multiple inheritance and provides a device called mixin.
Mixins gives you the perfect way to add functionality to your class. However, they are really powerful when the code in mixin starts interacting with the code in the class that uses it.
Let's take a look at the following sample code to learn more about mixin:
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