A Module is a way of combining methods, classes, and constants. Modules provide you with two major benefits.
The module provides a namespace and avoids name conflicts.
The module is implemented
mixindevice.
The module defines a namespace, the equivalent of a sandboxie, in which your methods and constants do not conflict with method constants elsewhere.
Modules are similar to classes, but with the following differences:
Module cannot be instantiated
Module has no subclasses
A module can only be defined by another module.
6.20.1. Grammar #
moduleIdentifierstatement1statement2...........end Module constant naming is similar to class constant naming, starting with anuppercase letter. Method definitions also look similar: module method definitions are similar to class method definitions.
With class methods, you can call a module method by placing a module name and a period before the class method name, and you can use the module name and two colons to refer to a constant.
6.20.2. Example #
#!/usr/bin/ruby#Defined in trig. rb Module in file moduleTrigPI=3.141592654defTrig.sin(x)#..enddefTrig.cos(x)#..endend We can define multiple modules with the same function name but different functions:
6.20.3. Example #
#/ Usr/bin/ruby # defined in moral. rb Module MoralVERY in file_ BAD=0BAD=1defMoral. sin (badness) #... end Just like class methods, when you define a method in a module, you can specify that the module name is followed by a period followed by the method name. The require statement is similar to the include statement in C and C++ and the Ruby
require statement # import statement. If a third-party program wants to use any definedmodule, it can simply use Ruby require statement to load the module file:Grammar #
6.20.4. Grammar #
requirefilename Here, the file extension .rb is not necessary.
6.20.5. Example #
$LOAD_PATH<<'.'require'trig.rb'require'moral'y=Trig.sin(Trig::PI/4) wrongdoing = Moral.sin(Moral::VERY_BAD) Here, we use the $LOAD_PATH << '.' , make Ruby know that referenced files must be searched in the current directory. If you don’t want to use the $LOAD_PATH , then you can use the require_relative to reference a file from a relative directory.
Note: here, the file contains the same function name. So, this leads to codeambiguity when referencing the calling program, but the module avoids this code ambiguity, and we can call the appropriate function with the name of the module. You can embed a module in a class. To embed a module in a class, you can usethe Ruby include statement #
include statement:
6.20.6. Grammar #
includemodulename If the module is defined in a separate file, you need to use the Suppose the following module is written in the You can now reference the module in the class, as follows: require statement refers to the file.Example #
support.rb file.moduleWeekFIRST_DAY="Sunday"defWeek.weeks_in_monthputs"You have four weeks in a month"enddefWeek.weeks_in_yearputs"You have 52 weeks in a year"endend
6.20.7. Example #
#!/usr/bin/ruby$LOAD_PATH<<'.'require"support"classDecadeincludeWeekno_of_yrs=10 defno_of_monthsputsWeek::FIRST_DAYnumber=10*12putsnumberendendd1=Decade.newputs Week::FIRST_DAYWeek.weeks_in_monthWeek.weeks_in_yeard1.no_of_months This will produce the following results:
Sunday You have four weeks in a month You have 52 weeks in a year Sunday 120 Ruby Mixins #
Before reading this section, you need to have a preliminary understanding ofobject-oriented concepts.
When a class can inherit the properties of a class from more than one parentclass, the class appears as multiple inheritance.
Ruby does not directly support multiple inheritance, but Ruby’s module (Module) has another magical function. It almost eliminates the need for multiple inheritance and provides a method called mixin device.
Ruby does not really implement the multiple inheritance mechanism, but instead uses the mixin technology as a substitute. Put the module include in the class definition, the methods in the module are mix into the class.
Let’s take a look at the following sample code to learn more about mixin :
6.20.8. Example #
moduleAdefa1enddefa2endendmoduleBdefb1enddefb2endendclassSampleincludeAincludeBdef s1endendsamp=Sample.newsamp.a1samp.a2samp.b1samp.b2samp.s1 Module A consists of methods A1 and a2.
Module B consists of methods b1 and b2.
Class
Samplemodules A and B are included.Class
Samplecan access all four methods, namely A1, a2, b1, and b2.