Open In App

Ruby | Module

Last Updated : 10 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

A Module is a collection of methods, constants, and class variables. Modules are defined as a class, but with the module keyword not with class keyword.

Important Points about Modules:

  • You cannot inherit modules or you can’t create a subclass of a module.
  • Objects cannot be created from a module.
  • Modules are used as namespaces and as mixins.
  • All the classes are modules, but all the modules are not classes.
  • The class can use namespaces, but they cannot use mixins like modules.
  • The name of a module must start with a capital letter.

Syntax:

module Module_name

   # statements to be executed

end

Example:




# Ruby program to illustrate 
# the module
    
# Creating a module with name Gfg
module Gfg
      
    C = 10;
    
    # Prefix with name of Module
    # module method 
    def Gfg.portal
        puts "Welcome to GFG Portal!"
    end
        
    # Prefix with the name of Module
    # module method
    def Gfg.tutorial  
        puts "Ruby Tutorial!"
    end
        
    # Prefix with the name of Module
    # module method
    def Gfg.topic  
        puts "Topic - Module"
    end
      
end
   
# displaying the value of 
# module constant
puts Gfg::C
  
# calling the methods of the module
Gfg.portal
Gfg.tutorial
Gfg.topic


Output:

10
Welcome to GFG Portal!
Ruby Tutorial!
Topic - Module

Note:

  • To define module method user have to prefix the name of the module with the method name while defining the method. The benefit of defining module method is that user can call this method by simply using the name of module and dot operator as shown in above example.
  • A user can access the value of a module constant by using the double colon operator(::) as shown in the above example.
  • If the user will define a method with def keyword only inside a module i.e. def method_name then it will consider as an instance method. A user cannot access instance method directly with the use of the dot operator as he cannot make the instance of the module.
  • To access the instance method defined inside the module, the user has to include the module inside a class and then use the class instance to access that method. Below example illustrate this concept clearly.
  • The user can use the module inside the class by using include keyword. In this case, the module works like a namespace.

Example:




# Ruby program to illustrate how 
# to use module inside a class
   
# Creating a module with name Gfg
module Gfg
   
    # module method 
    def portal
        puts "Welcome to GFG Portal!" 
    end
       
    # module method
    def tutorial  
        puts "Ruby Tutorial!" 
    end
       
    # module method
    def topic  
        puts "Topic - Module" 
    end
      
end 
  
  
# Create class
class GeeksforGeeks
   
    # Include module in class
    # by using 'include' keyword
    include Gfg
       
    # Method of the class
    def add
        x = 30 + 20
        puts x
    end
      
end
       
# Creating objects of class 
obj_class =  GeeksforGeeks.new
       
# calling module methods
# with the help of GeeksforGeeks
# class object
obj_class.portal 
obj_class.tutorial
obj_class.topic
  
# Calling class method 
obj_class.add 


Output:

Welcome to GFG Portal!
Ruby Tutorial!
Topic - Module
50

Use of Modules: A module is a way categorize the methods and constants so that user can reuse them. Suppose he wants to write two methods and also want to use these methods in multiple programs. So, he will write these methods in a module, so that he can easily call this module in any program with the help of require keyword without re-writing code.

Example:




# Ruby program for creating a module
  
# define module
module Gfg
  
    # module method 
    def Gfg.portal()
          
        puts "Module Method 1"  
    end
      
    # module method 
    def Gfg.tutorial()
          
        puts "Module Method 2"  
    end
  
end  


Note: Save this program in a file named as my.rb. You can use this into another program with the help of require keyword. This is like including the header files in C/C++ program.

Using Command:

ruby my.rb

Now create a another file containing the following code:




# Ruby program to show how to use a 
# module using require keyword
  
# adding module 
require "./my.rb"
  
# calling the methods of module Gfg
Gfg.portal()
Gfg.tutorial()


Save this file as my1.rb.

Using Command:

ruby my1.rb

Output:

Module Method 1
Module Method 2


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads