Open In App

Ruby | Module

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:



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:

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

Article Tags :