Updated March 31, 2023
Introduction to Ruby Modules
Modules in Ruby are the combination of the class, methods and constant; modules in Ruby looks like any other class, but it is not like the class because we cannot inherit modules (which means we cannot create a subclass from the modules), modules can be used in Ruby as the namespace and mixin, names of modules in Ruby is always starting with the capital letters, and if anyone wants to access modules constant they can do it with a double colon(::) operator, the main benefits of using modules in the Ruby is it allows us to access directly with module name to its attribute instead of accessing with the object of the module.
Syntax
Syntax of the module is very simple and clearly puts everything inside the module. The attributes can be constant, class and methods. We can explain the below syntax in the following steps.
- We have defined a module Test; here, we can see that the name of the module starts with the capital letter. Which means the name of the Module will start with the capital letter only.
- The module contains the constant and the method Cons and method1.
- Anyone can directly access the constant Cons and method method1 by the name of the module, which is Test.Cons and Test.method1.
- With the help of the modules, we can create separate data and methods for the different modules and can be directly accessed with the names of the modules.
module Test(start with capital letter)
# Statement and all its attributes goes here.
#Constant Variable
Cons =100
#Method definition
def Test.method1
#code to be executed
end
end
Functions of Modules in Ruby
Modules are like namespace and mixin in Ruby; it allows the developer to define some scope that can contain the constant, method and class altogether, and the developer can easily access the attributes of the modules with the name of the modules directly. First, let us understand the working and some key concepts of the modules in Ruby.
- The module looks like any other class, but it is far more different than any class; the biggest difference is we cannot create a subclass from the modules; we can directly use the module for our uses.
- Another important thing which we already discussed is the module; we do not have to create an object of the module we can directly access with the name of the modules. Even modules do not allow us to create the Objects.
- If you have heard about the namespace (which allows you to define the unique name for any set of codes), modules are used as the namespace.
- One important fact about the module working is that all the classes are modules, but not all are classes.
- We can use the module for the mixin purpose, which means adding one or more classes to it without using inheritance concepts.
Examples to Implement of Ruby Modules
Below are the examples of Ruby Modules:
Example #1
This is an example where we are creating a module, and inside this module, we are defining a few methods and a constant and then we are directly accessing these methods and constants with the name of the modules. So we can explain the below example in the following steps.
- We have created a Module with the name Test.
- Inside the module Test, we have defined CONS as a constant and three methods greetings, Goodby and take care.
- Finally, in the last step, we are directly calling the methods and accessing the constant.
Please follow the below example along with the output of the screen:
Code:
# Here we are creating a Module which contains constants and methods
module Test
# defining the constant in this module
CONS = 20;
# Defining the method greetings in the Test modules
def Test.greetings
puts "Welcome to Test module friends"
end
# Defining the method goodbye in the Test modules
def Test.Goodby
puts "Goodby to all from Test Module"
end
# Defining the method takecare in the Test modules
def Test.takecare
puts "Take care of you friends"
end
end
#Printing the value of the constant of the Test module.
puts Test::CONS
# Here we are accessing the constant and the method of the module with the name of module Test.
Test.greetings
Test.Goodby
Test.takecare
Output:
Example #2
This is an example where we are creating a module and including this module inside another class (so that class can be able to access all the methods of the modules). Inside the class, we have defined a method sayHi, and some methods are defined inside the module Test, and finally class includes the module inside it. Now the class is easily able to access the methods of the module. We can explain the code flow in the below steps.
- We have created a Module with the name Test.
- Inside the module Test, we have defined CONS as a constant and three methods greetings, Goodby and take care.
- We have again created a class, TestClass, which includes the module inside it. Which means all the methods of the module can be called with the object of the class.
- Finally, in the last step, we are directly calling the methods of module Test and method of class TestClass itself with the object of ClassTest.
Please follow the below example along with the output of the screen:
Code:
# Here we are creating a Module which contains constants and methods
module Test
# defining the constant in this module
CONS = 20;
# Defining the method greetings in the Test modules
def greetings
puts "Welcome to Test module friends"
end
# Defining the method Goodby in the Test modules
def Goodby
puts "Goodby to all from Test Module"
end
# Defining the method take care in the Test modules
def takecare
puts "Take care of you friends"
end
end
# Creating a class which will holds the module(include the module)
class TestClass
# Include module in class
# by using 'include' keyword
include Test
# Method of the class
def sayHi
puts "Welcome you inside TestClass which includes Test Module"
end
end
# Creating objects of class TestClass
test_class_object = TestClass.new
#Here we are calling all the methods of the module with the class object.
test_class_object.greetings
test_class_object.Goodby
test_class_object.takecare
# Calling class method
test_class_object.sayHi
Output:
Conclusion
From these tutorials, we learned about the basics of the modules in Ruby, we also learned about the working of the modules, we came to know that we can use modules directly or one module can be included in another class and from that class, we can use the module attributes.
Recommended Article
We hope that this EDUCBA information on “Ruby Modules” was beneficial to you. You can view EDUCBA’s recommended articles for more information.