The Wayback Machine - https://web.archive.org/web/20240815035118/https://www.geeksforgeeks.org/ruby-module/
Open In App

Ruby | Module

Last Updated : 10 Oct, 2018
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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


Previous Article
Next Article

Similar Reads

Ruby | Comparable Module
In Ruby, the mixin of Comparable is used by the class whose objects may be ordered. The class must be defined using an operator which compare the receiver against another object. It will return -1, 0, and 1 depending upon the receiver. If the receiver is less than another object, then it returns -1, if the receiver is equal to another object, then
3 min read
Ruby | Math Module
In Ruby, Modules are defined as a collection of methods, classes, and constants together. Math module consists of the module methods for basic trigonometric and transcendental functions. Module ConstantsNameDescriptionEDefine the value of base of natural logarithm e.PIDefine the value of π. Example: C/C++ Code # Ruby code to illustrate the # Math M
4 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Private Classes in Ruby
The concept of private, protected and public methods in Ruby is a bit different than it other languages like Java. In Ruby, it is all about which class the person is calling, as classes are objects in ruby. Private Class When a constant is declared private in Ruby, it means this constant can never be called with an explicit receiver, a private cons
3 min read
Ruby | Matrix cofactor() function
The cofactor() is an inbuilt method in Ruby returns the cofactor of the given index. Syntax: mat1.cofactor(i, j) Parameters: The function needs two index i and j, whose cofactor is to be returned. Return Value: It returns the cofactor of the given index. Example 1: # Ruby program for cofactor() method in Matrix # Include matrix require "matrix
1 min read
Ruby Integer odd? function with example
The odd? function in Ruby returns a boolean value. It returns true if the number is odd, else it returns false. Syntax: number.odd? Parameter: The function takes the integer which is to be checked for odd or not. Return Value: The function returns a boolean value which determines if the value is odd or not. Example #1: # Ruby program of integer odd
1 min read
Ruby Integer div() function with example
The div() function in Ruby returns the integer division of two numbers. Syntax: (number1).div(number2) Parameter: The function needs two numbers number1 and number2, where number1 is the dividend and number2 is the divisor. Return Value: The function returns the integer division of two numbers. Example #1: # Ruby program of Integer div() function #
1 min read
Ruby | Symbol to_s function
Symbol#to_s() : to_s() is a Symbol class method which returns string representation of the symbol object. Syntax: Symbol.to_s() Parameter: Symbol values Return: string representation of the symbol object. Example #1 : # Ruby code for Symbol.to_s() method # declaring Symbol a = :aBcDeF # declaring Symbol b = :"\u{e4 f6 fc}" # declaring Sym
1 min read
Ruby | Set replace() function
The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Syntax: s1.replace(enum) Parameters: The function accepts an enumerable object which to be replaced in the set. Return Value: It returns the self object which contains the contents of the set after rep
1 min read
Ruby | Set reset() function
The reset() is an inbuilt method in Ruby resets the internal state after modification to existing elements and returns self. The elements will be reindexed and deduplicated. Syntax: s1.reset() Parameters: The function does not accepts any parameter. Return Value: It returns self . Example 1: # Ruby program to illustrate # the reset() method # requi
1 min read
Article Tags :
three90RightbarBannerImg