Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require a ruby file from another directory

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

like image 667
thedarkknight228 Avatar asked May 31 '13 11:05

thedarkknight228


People also ask

How do I use require in Ruby?

The require method takes the name of the file to require, as a string, as a single argument. This can either be a path to the file, such as ./lib/some_library. rb or a shortened name, such as some_library. If the argument is a path and complete filename, the require method will look there for the file.

How do I change the path of a file in Ruby?

Ruby has a method for this case. It is File::expand_path . Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point.

What does require relative mean Ruby?

require_relative allows you to "load a file that is relative to the file containing the require_relative statement". With require , ./ indicates a path that is relative to your current working directory. – Ajedi32.

What does __ file __ mean in Ruby?

The value of __FILE__ is a relative path that is created and stored (but never updated) when your file is loaded. This means that if you have any calls to Dir.


1 Answers

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:

  • $LOAD_PATH
  • require
  • require_relative
  • File.dirname
  • __FILE__
like image 173
Charles Caldwell Avatar answered Sep 20 '22 15:09

Charles Caldwell