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.
module Module_name # statements to be executed end
10 Welcome to GFG Portal! Ruby Tutorial! Topic - Module
- 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.
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.
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.
Now create a another file containing the following code:
Save this file as my1.rb.
Module Method 1 Module Method 2
- Private Classes in Ruby
- Ruby | Matrix cofactor() function
- Ruby Integer odd? function with example
- Ruby Integer div() function with example
- Ruby | Symbol to_s function
- Ruby | Set replace() function
- Ruby | Set reset() function
- Ruby For Beginners
- Ruby | Array Concatenation using (+) function
- Ruby | at() function
- Ruby Float to_r() method with example
- Ruby | Keywords
- Ruby | Exception Class and its Methods
- Ruby | String inspect Method
- Ruby | Numeric imaginary() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.