6.20. Ruby module

发布时间 :2023-10-26 23:00:07 UTC      

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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.