Updated April 4, 2023
Definition of Ruby Require
In Ruby to reuse any existing files and modules we can use the require statement, required to allow us to make available the submodules inside our codes, there are two way to require any file first way by giving the full path of the file like require ‘./dire/lib/xyz.rb’ in this approach, we are giving the full path of the library, another way is directly giving the file name like require ‘xyz.rb’ instead of mentioning the full path which also called as shortening the path, in this approach application will search for the name inside all the predefined directory, writing directly file name is more common for require statement.
Syntax of Ruby Require
Below is the syntax for the require in Ruby, to explain the syntax we are taking two files, one file which will play the role of the library and another file which uses the library file by using the require statement.
We can explain the syntax of the library files first by the below steps.
- Inside the library syntax, we can see that we have created two files called library1.rb and library2.rb.
- Next inside the library files we have initialized and created class and methods inside the class.
- Inside initialize we will do the initialization part of the class and the method will perform our required activity. We can write some codes inside it the methods and initialize as per our requirements.
- Next is our file which will use the rb and library1.rb files which is use.rb. These library files will allow the use.rb file to use all the methods and classes of the library1.rb and library2.rb files.
- Finally, we are creating an object of class LibraryClass1 and LibraryClass2 which is available inside the library1.rb and library2.rb file respectively which we have required inside the rb file.
See the files and syntax written on the files.
1. Library file (library1.rb)
class LibraryClass1
def initialize
#Do some initialisation for the class before calling any function of the class
end
def library_method1
#Here write some code for the method
end
End
2. Library file (library2.rb)
class LibraryClass2
def initialize
#Do some initialisation for the class before calling any function of the class
end
def library_method2
#Here write some code for the method
end
end
3. File which will use the Library file(use.rb)
require 'library1.rb'
require 'library2.rb'
libraryObj1 = LibraryClass1.new
libraryObj2 = LibraryClass2.new
How does a Required Statement Work in Ruby?
To explain the working flow of the required statement we will take the help of the below diagram. We can explain the flow of the below diagram from the following steps.
- Inside the below diagram we can see that we have created two files called library1.rb and library2.rb.These files will play the role of the library
- Next inside the library files, we have created a class and inside the class, we have methods initialized. Inside the method, we can put some logic, and inside the initialize we can put the initialization works.
- Inside initialize we will do the initialization part of the class and the method will perform our required activity. We can write some codes inside it the methods and initialize as per our requirements.
- Next is our file which will use the rb and library1.rb files which is use.rb .These library files will allow the use.rb file to use all the methods and classes of the library1.rb and library2.rb files.
- Finally, we are creating an object of class LibraryClass1 and LibraryClass2 which is available inside the rb and library2.rb file respectively which we have required inside the use.rb file.
Examples to Implement Require in Ruby
To explain the below example we can follow the below steps.
- In the below example we can see that we have created two files called rb and library2.rb.These files will play the role of the library
- Next inside the library files, we have created a class and inside the class, we have methods initialized. Inside methods, we have written puts to print the message.
- Next is our file which will use the rb and library1.rb files which is use.rb.These library files will allow the use.rb file to use all the methods and classes of the library1.rb and library2.rb files.
- Finally, we are creating an object of class LibraryClass1 and LibraryClass2 which is available inside the rb and library2.rb file respectively which we have required inside the use.rb file.
Please see the below example along with the screen of the output.
File library1.rb
puts "Library1"
class LibraryClass1
def initialize
#Do some initialisation for the class before calling any function of the class
end
def library_method1
puts "Welcome the library1 file , I am available for use.rb"
end
end
File library2.rb
puts "Library2"
class LibraryClass2
def initialize
#Do some initialisation for the class before calling any function of the class
end
def library_method2
puts "Welcome the library2 file , I am available for use.rb"
end
end
File use.rb
require './library1.rb'
require './library2.rb'
libraryObj1 = LibraryClass1.new
libraryObj2 = LibraryClass2.new
libraryObj1.library_method1
libraryObj2.library_method2
Output:
Conclusion
From these tutorials we learned about the basics of the requires in Ruby, we also learned about the working of the requires with help of a diagram, we came to know that we can use requires where we wanted to use the features of other files, these features may be class and method of any library files which inside our files where we required these library files.
Recommended Articles
We hope that this EDUCBA information on “Ruby Require” was beneficial to you. You can view EDUCBA’s recommended articles for more information.