Ruby module


Release date:2023-10-27 Update date:2023-10-27 Editor:admin View counts:225

Label:

Ruby module

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.

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.

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:

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

Grammar

requirefilename

Here, the file extension .rb is not necessary.

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:

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:

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 :

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.

Powered by TorCMS (https://github.com/bukun/TorCMS).