Ruby program to demonstrate a simple module

Ruby Example: Write a program to demonstrate a simple module.
Submitted by Nidhi, on December 29, 2021

Problem Solution:

In this program, we will create a module and define some methods inside the method. Then we will call all methods defined inside the method.

Program/Source Code:

The source code to demonstrate a simple module is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate 
# a simple module

module MyModule
    def MyModule.Method1
        puts "Inside Method1";
    end
    
    def MyModule.Method2
        puts "Inside Method2";
    end
    
    def MyModule.Method3
        puts "Inside Method3";
    end
end

MyModule.Method1();
MyModule.Method2();
MyModule.Method3();

Output:

Inside Method1
Inside Method2
Inside Method3

Explanation:

In the above program, we created a module MyModule. Then we defined 3 methods Method1, Method2, and Method3 inside the created module. After that, we called all defined methods and printed appropriate messages.

Ruby Modules and Mixins Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.