Updated April 4, 2023
Definition of Ruby Include
In Ruby to reuse any existing module’s methods we can use the include statement, allowing us to make available all the methods of the module inside our class where we included the module, suppose we have a module which contains the methods m1 and m2 and we have another class which contains the method m3, now if we need some functionality inside the class and these functionalities are available inside the module’s method m1 and m2 in that case we do not require to write same functionality again inside the class, we can simply use the include and module name inside the class and all the methods of the module will be available.
Syntax:
Below is the syntax for the include statement in Ruby, we have created a module and including that module inside the class Class Name. Once we include the module inside the ClassName all the methods of the module will be included in the class.
See the files and syntax written on the files.
module ModuleName
class LibraryClass
def library_method
#Here write some code for the method
end
end
end
class ClassName
include ModuleName
end
How does an Include Statement Works in Ruby?
We can explain the functions of the include concept with the help of the diagram below, let me explain it step by step.
- When we write in Ruby, the compiler looks for the module name which we are including inside the class and include all the methods of the module inside the class.
- Once we have included the module all the methods can be directly accessed with a class name. Here class name is the class where we have included the module.
- In the below diagram we can see that we use the include inside the class C and all the methods of the module M are available for the class C
Please see the below diagram for better understanding.
Examples to Implement Include in Ruby
Following are the examples are given below:
Example #1
Below is a simple example where we are using a module with the name Human and a class HumanType. We can explain the below example with the following steps.
- First, we have created a Human module and it contains a method display_method. Here display_method is printing a message.
- Next, we have created a class called HumanType, which contains two methods: boy and girl. Here both boys and girls are printing some messages.
- Finally, inside the HumanType class, we have included the module Human.
- And we are able to access the method of module Human directly with the name of the class by using the new operator.
Please follow the below example along with the output.
Code:
#Defining module which contains methods.
module Human
def display_method
puts "Hi, I am First a human"
end
end
#This class is including the module inside it ,which means it will have all the methods of the module.
class HumanType
include Human
def boy
puts "Hey, I am boy"
end
def girl
puts "Hay, I am girl"
end
end
#Accessing the methods of the module with the name of the class using a new operator.
HumanType.new.display_method
HumanType.new.girl
HumanType.new.boy
Output:
Example #2
In the below example we are doing an arithmetic operation, all the methods for the arithmetic operations are written inside the module and a class called GeneralWork is including the module called Calculation inside it. We can explain the example in small steps.
- Module Calculation contains the method for the arithmetic operations.
- The class GeneralWork contains only one method which displays a simple message.
- Once we include the Calculation Module inside the class GeneralWork, the class will have all the methods of the module.
- You can see in the example we are accessing all the arithmetic operations with the name of class by using a new keyword.
Please follow the below example along with the output.
Code:
#Defining module which contains methods.
module Calculation
def add(a,b)
puts "The sum of the number is #{a+b}"
end
def substract(a,b)
puts "The sum of the number is #{a*b}"
end
def divide(a,b)
puts "The sum of the number is #{a/b}"
end
def multiply(a,b)
puts "The sum of the number is #{a*b}"
end
end
#This class is including the module inside it ,which means it will have all the methods of the module.
class GeneralWork
include Calculation
def general_work(a,b)
puts "Hay , we can perform arithmetic operation with #{a} and #{b}"
end
end
#Accessing the methods of the module with the name of the class using a new operator.
GeneralWork.new.general_work(2,4)
GeneralWork.new.add(2,4)
GeneralWork.new.substract(2,4)
GeneralWork.new.divide(2,4)
GeneralWork.new.multiply(2,4)
Output:
Example #3
In the below example we are calculating the area of various structures, and all the methods we have written inside the module and class will include the module inside the class and all the methods for arithmetic calculation will be captured into class.
Please follow the below example along with the output.
Code:
#Defining module which contains methods.
module Area
def square(a)
puts "The Are of the square is #{a*a}"
end
def rectangle(a,b)
puts "The area of the rectangle is #{a*b}"
end
def cube(a)
puts "The area of cube is #{6*a*a}"
end
def circle(a)
puts "The area of circle is #{3.14*a*a}"
end
end
#This class is including the module inside it ,which means it will have all the methods of the module.
class AreaCalculation
include Area
def area
puts "Hay , we can perform area calculation"
end
end
#Accessing the methods of the module with the name of the class using a new operator.
AreaCalculation.new.area()
AreaCalculation.new.square(2)
AreaCalculation.new.rectangle(2,4)
AreaCalculation.new.cube(4)
AreaCalculation.new.circle(2)
Output:
Conclusion
From these tutorials we learned about the basics of the include in Ruby, we also learned about the working of the include with help of a diagram, we came to know that we can use include where we wanted to use the methods of other modules inside any class, it will include all of the methods of the modules.
Recommended Articles
We hope that this EDUCBA information on “Ruby Include” was beneficial to you. You can view EDUCBA’s recommended articles for more information.