Home Ruby Tutorial Modules in Ruby Tutorial

Modules in Ruby Tutorial

66
0
SHARE
Modules Ruby Tutorial
Modules Ruby Tutorial

In this Ruby Tutorial we will discuss about Modules in Ruby.

The modules in Ruby are simply the wrappers around our Ruby code. But wait, then what was a class? The class was a way to gather different methods and attributes. Modules work the same way as class does but the one most important difference is they cannot be instantiated. That means we never have a instance of a module. But we have a instance of our class. Instead we use our modules in conjunction to our class. There are basically two reasons why we want to use modules in our class. Let me explain them

 

Namespaces

If you have a familiarity with another programming language then the namespace has a common goals.  Let’s take an example to understand the scenario. If you have two friends with same name Jacky. And you met both of them in a restaurant then it would be difficult for you to call them by name. You would say “Hey Jacky!”, but there becomes a confusion to them. They won’t know to whom you are referencing to. So you should say something like “Jacky A” and “Jacky B”. Namespace is similar things. If you have two classes with same name then to resolve the conflict you should use namespaces.

Now, let’s take a real world example of what it is about. Say we are developing a dating website. We have a class called Date to set up the dinner between two people that are connected to. So this class conflicts with the standard ruby class Date that ruby uses to fetch date and time.

class Date

end

 

the_dinner = Date.new

the_dinner.date = Date.new

The first line here tells Ruby to create a new Date object and the second one tells to create a time and date for the dinner. Ruby won’t know the difference in such case.

Confusing right? So to resolve this we need a module to namespace our code. So let’s try this with modules.

Let’s wrap our code to the module.

module Interesting

 class Date

        …

 end

end

Now let’s do this

the_dinner = Interesting::Date.new

the_dinner.date = Date.new

 

Wow! we resolved the confusion isn’t it? The modules are named the same way the classes are but it goes as the wrapper inside the class. The double colon (::) tells ruby that it is namespaced. Also modules are used in open source projects so that our class don’t conflict to their’s class because we can’t tell ahead of time what class they are going to create.

 

In a nutshell Namespaces can

  • Keep class name distinct from standard Ruby classes
  • Disambiguate our own class defination
  • Classes in open sources don’t conflict

 

MixIns

Unlike any other programming languages Ruby won’t let us do multiple inheritance in our class. It only allows us to inherit from one super class. If we need another functionality that needs to brought in then we put it into modules and mix in. That is important to keep our class organized. Let’s take an example

class Person

attr_accessor :first_name, :last_name

def full_name

  return @first_name + “ “ + @last_name

end

end

 

 

# Say now we need another classes that are similar

 

class Professor

 

end

 

 

class Student

 

end

 

class Staff

 

end

 

All of the classes Person, Professor, Student and Staff needs that method called first_name. Then what would you? Copy the method and put it in all class? It is a terrible idea. In that case we create a new module called Nameinfo and mix in to our classes that require them

module Nameinfo

attr_accessor :first_name, :last_name

def full_name

  return @first_name + “ “ + @last_name

end

end

 

Now in our classes we do this

class Person

include Nameinfo

end

 

class Professor

include Nameinfo

end

 

class Student

include Nameinfo

end

 

class Staff

include Nameinfo

end

 

So this is same as writing all our class methods inside the class. We can now access all these methods from all classes. Happy? 🙂

Modules are also inheritable see the example below, we can add other attributes to the classes and so on.

class Person

include Nameinfo

end

 

class Professor

include Nameinfo

end

 

class Student

include Nameinfo

end

 

# The staff class inherits from person class

class Staff < Person

attr_accessor :age

 

def some_method

   ….

 end

 

end

 

Load, Require and Include

These three command in Ruby are used when we have a module in other file as the code library and we want to import it to our class before we try to mix in. The major differences are if you load the module then you need to load it every single time you need the module.

 

load ‘Nameinfo’

 

class Person

# include Nameinfo

end

 

load ‘Nameinfo’

class Professor

#include Nameinfo

end

 

load ‘Nameinfo’

class Student

#include Nameinfo

end

 

load ‘Nameinfo’

class Staff

#include Nameinfo

end

 

If you use require, Ruby is going to know if you have already loaded the file or not. If you have already done that, it won’t load the module next time. So it is the better way to do it

require ‘Nameinfo’

 

class Person

# include Nameinfo

end

 

class Professor

#include Nameinfo

end

 

class Student

#include Nameinfo

end

 

 

class Staff

#include Nameinfo

end

 

You know what include does, it includes the module.

In summary

Load => Loads the module every single time Ruby sees the load command

Require => Checks if the module has been loaded or not, if loaded it won’t load the file again

Include =>  Loads the module inside our class defination.

 

Enumerable as MixIns

The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max, #min, or #sort is used, the objects in the collection must also implement a meaningful <=> operator, as these methods rely on an ordering between members of the collection.

 

Conclusion

So modules in Ruby are one of the essential part of programming. They help us write less code and do more. All these advantages help in developing open source projects and contributions. There are lot of things you can do with these powerful techniques. If you want to learn more about modules and how they work you can refer to the ruby documentation https://ruby-doc.org/core-2.4.1/Enumerable.html

Want to know how we use Rails to build stock market application? click that link

SHARE
Previous articleGeolocation Rails Tutorial
Next articleHash and Symbols – Ruby on Rails Tutorial
mm
Taichi lover, book addicts, lost himself in the realm of writing code and poetry. Want to talk more? Need help on Ruby on Rails? we can help :)

LEAVE A REPLY

Please enter your comment!
Please enter your name here