Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby Module


May 12, 2021 Ruby


Table of contents


Ruby Module

Module is a way to combine methods, classes, and constants. Modules offer you two main benefits.

  • The module provides a namespace and avoids name conflicts.
  • The module implements the mixin unit.

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:

  • The module cannot be instantiated
  • The module has no sub-classes
  • A module can only be defined by another module
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.

Ruby require statement

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:

Grammar

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.

Ruby include statement

You can embed modules in classes. In order to embed modules in a class, you can use the include statement in the class:

Grammar

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

Mixins in Ruby

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

  • Module a consists of methods a1 and a2.
  • Module B consists of methods b1 and b2.
  • Class Sample contains modules A and B.
  • Class Sample has access to all four methods, a1, a2, b1, and b2.
  • Therefore, you can see that class Sample inherits two modules, and you can say that class Sample uses multiple inheritance or mixin.