6.20. Ruby module

发布时间 : 2025-10-25 13:34:05 UTC      

Page Views: 9 views

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 mixin device.

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.

Ruby require statement #

The require statement is similar to the include statement in C and C++ and the 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.

Ruby include statement #

You can embed a module in a class. To embed a module in a class, you can usethe include statement:

6.20.6. Grammar #

includemodulename 

If the module is defined in a separate file, you need to use the require statement refers to the file.

Example #

Suppose the following module is written in the 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 

You can now reference the module in the class, as follows:

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 Sample modules A and B are included.

  • Class Sample can access all four methods, namely A1, a2, b1, and b2.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。