Ruby Modules and Mixins Tutorial

In this section we will learn what modules are and how to use them in Ruby.

What is Modules in Ruby?

Ruby modules are a way of grouping related methods in one place and then pass the module to any class that might need those methods.

Basically, when working with inheritance in Ruby, we mentioned that the first rule to follow is that the child class must have the is-a relation with its super class! For example, a class like Dog can inherit from a class like Animal! Because after all a Dog is an Animal! So it can inherit from the Animal class and use its methods!

But we can’t make a class to inherit from another one just for the sake of using its methods while there’s no relation between the two classes! For example, a Car that inherits from a class like Animal just to use one or two of its methods! This is wrong!

But if there are methods that we know are general enough to be used in multiple classes that are unrelated to each other, then we can put those methods in a module and then import the module in any class that might need those methods.

That’s the purpose of modules!

Alright, now let’s see how to create and use modules in Ruby.

How to create a Module in Ruby?

This is the syntax on creating a module in Ruby:

module ModuleName

#instructions…

end

The process of creating a module in Ruby is exactly the same as the way we create a class in Ruby with the exception that we use the keyword module instead of class.

Now within the body of the module we can define as many method as we want.

Example: creating a Module in Ruby

module PrintFullName
def print_name
puts first_name, last_name, age, id
end
end

Here the name of the module is PrintFullName and it has one method named print_name. So any class that mixin this module will have the specified method as part of its instance methods and hence the objects we create from that class will have the access to the print_name method as well.

Mixin Modules in Ruby

The process of adding a module into a class is called Mixin!

This is the syntax on how we can mixin a module to a class:

class ClassName

include ModuleName

end

Within the body of a class and outside of any instance method of that class we call the include keyword and after that set the name of the target module we want to use its methods for the objects that will be created from the specified class.

Example: including modules in Ruby

module PrintFullName
def print_name
puts first_name, last_name, age, id
end
end
class Person
include PrintFullName
attr_accessor :first_name, :last_name, :age, :id
def initialize (first_name, last_name, age, id)
self.first_name = first_name
self.last_name = last_name
self.age = age
self.id = id
end
end
prs = Person.new("John","Doe",100, 43221)
prs.print_name
Output:
John
Doe
100
43221

How do modules work in Ruby?

In this example you can see we’ve included the PrintFullName module into the Person class and so now any object we create from the Person class will have the access to the methods of this module as well.

That’s how we could use the prs object and call the print_name method.

More to Read:

Ruby modules and inheritance