A Module is a way of combining methods, classes, and constants together. Modules provide two major benefits:
- Modules provide a namespace and prevent name clashes.
- Modules implement the mixin facility.
A Module defines a namespace, which is like a sandbox where your methods and constants won't conflict with those elsewhere.
Modules are similar to classes, but with the following differences:
- Modules cannot be instantiated.
- Modules have no subclasses.
- Modules can only be defined within another module.
Syntax
module Identifier
statement1
statement2
...........
end
Module constants are named similarly to class constants, starting with an uppercase letter. Method definitions also look similar: module method definitions are similar to class method definitions.
Module methods can be called by placing the module name followed by a dot before the method name. Constants can be referenced using the module name followed by two colons.
Example
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 names but different functionalities:
Example
module Moral
VERY_BAD = 0
BAD = 1
def Moral.sin(badness)
...
end
end
Just like class methods, when you define a method within a module, you can specify the module name followed by a dot, and then the method name.
The require statement is similar to the include statement in C and C++, or the import statement in Java. If a third-party program wants to use any defined module, it can simply use the Ruby require statement to load the module file:
Syntax
require filename
Here, the file extension .rb is not required.
Example
$LOAD_PATH << '.'
require 'trig.rb'
require 'moral'
y = Trig.sin(Trig::PI/4)
wrongdoing = Moral.sin(Moral::VERY_BAD)
Here, we use $LOAD_PATH << '.' to let Ruby know that it must search for the referenced files in the current directory. If you don't want to use $LOAD_PATH, you can use require_relative to reference files from a relative directory.
Note: Here, the files contain the same function names. This would cause ambiguity when calling from the referencing program, but modules avoid this ambiguity, and we can call the appropriate function using the module name.
You can embed a module within a class. To embed a module within a class, you can use the include statement in the class:
Syntax
include modulename
If the module is defined in a separate file, you need to use the require statement to reference that file before embedding the module.
Example
Assume 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
Now, you can reference this module in a class as shown below:
Example
$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 will produce the following result:
Sunday
You have four weeks in a month
You have 52 weeks in a year
Sunday
120
Before reading this section, you need to have a basic understanding of object-oriented concepts.
When a class can inherit characteristics from multiple parent classes, the class is said to show multiple inheritance.
Ruby does not directly support multiple inheritance, but Ruby's Modules have another magical feature. It almost eliminates the need for multiple inheritance by providing a facility called mixin.
Ruby does not truly implement multiple inheritance; instead, it uses a technique called mixin as an alternative. By including a module in a class definition, the methods of the module are mixed into the class.
Let's look at the following example code to understand mixin in depth:
Example
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 includes modules A and B.
- Class Sample can access all four methods: a1, a2, b1, and b2.
Therefore, you can see that class Sample inherits from two modules. You can say that class Sample uses multiple inheritance or mixin.
YouTip