How to require a ruby file from another directory
Solution 1
require
will search for files in only a set of locations referred to as the "Load Path." You can view the load path by using the global variable $LOAD_PATH
in a script or irb session. If it is not in the load path, it won't find it.
Ruby 1.9 introduced require_relative
which searches using the current file's location as a starting point.
# Will search $LOAD_PATH for file.
require 'test/unit'
# Notice the '/' which tells it to look in the
# 'test' folder for a file named 'unit.rb'
# Will look in current folder of file
require_relative 'my_folder/my_file'
# Will search in 'my_folder' for the file 'my_file.rb'
Note that require_relative
will not work in irb.
Also note, that if you really want to use require
, you can start your script by adding a location to the $LOAD_PATH
variable.
$LOAD_PATH << File.join('users', 'yourusername', 'your_folder')
# or
$LOAD_PATH << File.dirname(__FILE__)
# The second one enables you to move the file around on your
# system and still operate correctly
require 'my_file'
Here's some additional documentation from Ruby-Doc:
Solution 2
Also possible to do so:
require './something.rb'
from current directory to include.
Solution 3
From the docs: It suggests to use require relative. 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.
When you use require
to load a file, you are usually accessing functionality that has been properly installed, and made accessible, in your system. require does not offer a good solution for loading files within the project’s code. This may be useful during a development phase, for accessing test data, or even for accessing files that are "locked" away inside a project, not intended for outside use.
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"
Solution 4
For example, you have files:
chess.rb and spec/test.rb
And you want to require the file in spec/test.rb. Use:
require_relative '../test.rb'
with two dots. With one dot './' you will have an error "cannot load such file"
thedarkknight228
Updated on November 07, 2020Comments
-
thedarkknight228 over 3 years
I am trying to require a rake file that I have created inside another file I have. These two files are inside two different directories. I have require at the top of my first file with the name of the second file inside the quotes after the require. It is telling me that it can't load such file. Does that mean because its in different directory it can't find it? I tried sticking in the full path to the second file but it still can't load the file. Does anyone know how I can load the second file into the first?
Thanks in advance
-
thedarkknight228 almost 11 yearsThanks for a great answer, but just one question what goes inside the brackets? Is it the name of user, the username and the filepath to the folder
-
Charles Caldwell almost 11 yearsThat was just an example. I used that as an example Linux's home directory. It could just as easily be
File.join('C:', 'Users', 'Charles', 'blahblah')
or evenFile.dirname(__FILE__)
which will add the folder of where your current file is located to the load path.dirname
is probably better since it will enable you to move the file around and still work. -
Charles Caldwell almost 11 yearsDoes your Rake file end in '.rb' or '.rake'?
require
only looks for '.rb'. If it ends in '.rake' then you might consider looking at this SO question which talks about usingimport
. -
Charles Caldwell almost 11 yearsYou're welcome. There's a blog by Glenn Gentzke that mentions basically the exact same thing the SO question does. Interesting conundrum.
-
thedarkknight228 almost 11 yearsProbably stupid question but i'm getting undefined method 'import' error when I try to import .rake file. Would you know of an answer to it? Do I need to include something?
-
AlexSh over 5 yearsOr require_relative '../test'
-
Joshua Pinter about 4 yearsFYI, to see what is all included in your
LOAD_PATH
for your current environment, runputs $LOAD_PATH
in the Rails console.