Module in Ruby and its Usages in ‘Rails’ by Jay May 20, 2016 2 Minutes ReadIntroduction: Ruby is basically an Object Oriented Programming (OOP) language which is based upon various components such as classes, object, variables etc. It also provides a nice building block for applications which is known as module. A module is a collection of methods and constants. It defines a namespace in which other methods and constant can’t step on your methods and constants.Purpose of a module: Ruby module is a component to regroup similar things. Ruby methods, classes and constants can be grouped by similarity with the help of modules. Ruby modules provide two benefits:Modules provide ‘namespace’, it basically helps to prevent name clashes.Ruby’s ‘mixin’ facility is implemented with the help of modules.The basic syntax of module is: module Identifier statement1 statement2 ........... End Uses:Ruby module mainly functions as a namespace. It lets us define various methods for the actions that will perform. When a method defined inside a module does not clash with other methods that are written anywhere else, though they are having the same names.Module constants are named like class constants with an initial uppercase letter. Module methods are also defined like class methods. Here is an example. module MyModule def method puts “hello” end end To access the methods and constants inside a module in a class include key word is used. class Customer < ActiveRecord::Base include MyModule end To use the method that is defined inside a module, specify the module name followed by a dot and then the method name.Ruby Mixins and Ruby Modules:Ruby is purely an OOP language. But it does not support multiple inheritances directly, which is handled beautifully by Modules. They provide a facility called ‘mixin’ that eliminates the requirement of multiple inheritance. In Ruby when ‘mixins’ are called and used in proper manner they provide high degree of versatility functionality.A module ‘mixin’ generally consists of several lines of codes to set up conditions where the module can mix in with a class or classes to improve the functionality of the class or itself too. Here is an example of a module ‘mixin’. module A def a1 end def a2 end end module B def b1 end def b2 end end class MyClass include A include B def s1 end end obj = Objet.new obj.a1 obj.a2 obj.b1 obj.b2 obj.s1 Here module A and B consist of two methods individually. They are included in the classMyClass. Now MyClass can access all the four methods a1, a2, b1, b2. So it can be said that Myclass inherits from multiple modules. Thus multiple inheritances are implemented with the help of module’s ‘mixin’ facility.Conclusion:Modules don’t have any direct analogy in any mainstream programing language. They are used mostly in Ruby language. They are one of the key features making Ruby’s design beautiful. With the facility of ‘namespacing’ and ‘mixin’ modules make Ruby more flexible Object Oriented Programming language. They also make the application highly secure as classes and their objects inherit from modules and modules are having ‘mixins’.Planning something with RoR? We would love to get in touch with you. Related Posts: Ruby Rails Development Sphere & Associated Myths Why Businesses Prefer Ruby on Rails and Tips to Hire Ruby on Rails Developer SQL Injection (SQLi); How to Fix it in Ruby on RailsTags: Code, Rails, Ruby Jayadev Das email@example.comDo what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!