What is the difference between require_relative
and require
in Ruby?
-Auto_load: this initiates the method that is in hat file and allows the interpreter to call the method. -require_relative: allows the loading to take place of the local folders and files.
In Ruby, the require method is used to load another file and execute all its statements. This serves to import all class and method definitions in the file.
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.
Just look at the docs:
require_relative
complements the builtin methodrequire
by allowing you to load a file that is relative to the file containing therequire_relative
statement.For example, if you have unit test classes in the "test" directory, and data for them under the test "test/data" directory, then you might use a line like this in a test case:
require_relative "data/customer_data_1"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With