Module In Ruby And Its Usages In Rails

Module In Ruby And Its Usages In Rails
Social sharing

Ruby is an Object Oriented Programming (OOP) language. This programming language based upon various components such as classes, object, variables etc. Ruby also provides a nice building block for applications, and 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.

Contents

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.

Here is the Two benefits provide by the modules

  • Ruby provide ‘namespace’, and which 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’re having the same names.

Module constants are named like class constants with an initial uppercase letter. This are module methods, and 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.

Never miss an update from us. Join 10,000+ marketers and leaders.

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 programming 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.

Your recently viewed posts:

Jayadev Das - Post Author

Do 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 ‘About 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!

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box