Updated April 5, 2023
Introduction to Ruby Mixin
In case if we wanted to have multiple inheritance in Ruby then we can not use them directly, to use that concept we need to do it by the concept of the mixin, in the mixin concept we are using the modules concept, in one class we can include many modules and every modules contains some class and methods, which means we are able to access multiple inheritance here with the help of the modules and this methodology is called mixin in Ruby. We can use simple include keyword for including many modules inside a class and we know that every modules can have some class and methods.
Syntax:
Below is the simple syntax which shows the flow of the mixin in Ruby, here in the below syntax we have created a module called MIXIN and in this module we are including inside the MAINMODULE class. .
#Module1 defining with some methods
module MIXIN
def method1
end
def method2
end
end
class MAINMODULE
include MIXIN
defmain_method
end
end
How Mixin works in Ruby?
To see the working of the mixin in Ruby we will follow a diagram and some steps which will show the actual meaning of the mixin concept inside Ruby.
- First we have defined a module MIXIN1 and MIXIN2. We can put some class methods and constants inside the modules.
- Both of the modules MIXIN1 and MIXIN2 contain some methods like method1, method2, method3 and method4. These methods are going to be accessed by a single class without the concept of the multiple inheritance, simply this technique is called the mixin .
- Next we have defined a class MAINMODULE and this class includes the two above modules inside it. Once we include the modules inside the class, the class is eligible to have all the properties of the both modules (MIXIN1 and MIXIN2).
- Next we are creating the object of the MAINMODULE and with that object we are able to access the methods of the both modules which we have included into our MAINMODULE class.
- This mechanism of including and using two or more modules inside one class is nothing but a mixin.
- So we can understand from syntax it is replacing the need of multi inheritance in Ruby.
Given below is the diagram:
Examples of Ruby Mixin
Given below are the examples mentioned:
Example #1
In the below example we are performing arithmetic operations inside the module called CALCULATE and this module will be included inside the USECALCULATION class.
Code:
# Defining CALCULATE modules which consists of some methods for arithmetic operations.
module CALCULATE
def add(a,b)
puts "The sum of two number is #{a+b}"
end
def multi(a,b)
puts "The multiplication of two number is #{a*b}"
end
def div(a,b)
puts "The division of two number is #{a/b}"
end
defsubstract(a,b)
puts "The subtraction of two number is #{a-b}"
end
end
# Defining a main class method and inside the method we are simply including all the three modules which we have defined above.
class USECALCULATION
include CALCULATE
defdisplay_output
puts 'This is inside the USECALCULATION and method CALCULATE'
end
end
# Creating object
mainObject = USECALCULATION.new
# Calling methods
mainObject.display_output
mainObject.add(1,3)
mainObject.multi(2,4)
mainObject.div(2,4)
mainObject.substract(2,4)
Output:
Example #2
Code:
# Defining three modules which consists of some methods .
module MIXIN1
def method1
puts 'This is inside the MIXIN1 and method1.'
end
end
module MIXIN2
def method2
puts 'This is inside the MIXIN1 and method1.'
end
end
module MIXIN3
def method3
puts 'This is inside the MIXIN1 and method1.'
end
end
# Defining a main class method and inside the method we are simply including all the three modules which we have defined abbove.
class MAINMODULECLASS
include MIXIN1
include MIXIN2
include MIXIN3
defdisplay_main
puts 'This is indide the MAINMODULECLASS and method display_main'
end
end
# Creating object
mainObject = MAINMODULECLASS.new
# Calling methods
mainObject.display_main
mainObject.method1
mainObject.method2
mainObject.method3
Output:
Conclusion
From these tutorials we saw about the basics of the mixin in Ruby, we also saw about the working of the mixin with help of diagram, we came to know that we can use mixin where we need to include one or many modules into another class and from that class object we can access attributes of modules like its method.
Recommended Articles
We hope that this EDUCBA information on “Ruby Mixin” was beneficial to you. You can view EDUCBA’s recommended articles for more information.