Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between require_relative and require in Ruby?

Tags:

ruby

require

What is the difference between require_relative and require in Ruby?

like image 859
ab217 Avatar asked Sep 08 '10 23:09

ab217


People also ask

What is the purpose of load Auto_load and Require_relative 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.

What happens when you require a file in Ruby?

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.

What is require in Ruby on Rails?

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.


1 Answers

Just look at the docs:

require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_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" 
like image 158
miku Avatar answered Sep 23 '22 09:09

miku