Requiring a file or library in Ruby

Tech - RubyCademy
Apr 24, 2018 · 4 min read

In this article we’re going to explore the following topics:

  • the difference between an absolute and a relative path
  • the Kernel#require method
  • the Kernel#require_relative method

Before to start

With 50+ courses that cover various topics on Ruby and Ruby on Rails, it’s the best way to take your knowledge to the next level!

Try it for free 👇😉

Thank you for your time!

Absolute path vs Relative path

$> tree
.
├── file1.txt
├── directory/
│ └── file2.txt
├── another_directory/
│ └── file3.txt
2 directories, 3 files$> pwd
/Users/mehdi/path

Let’s cat the file3.txt file by using an absolute path

$> cat /Users/mehdi/path/another_directory/file3.txt
I'm the file #3
$> cd directory && pwd
/Users/mehdi/path/directory
$> cat /Users/mehdi/path/another_directory/file3.txt
I'm the file #3

First, we cat the file3.txt file by using an absolute path.

Then, we move to the directory subfolder.

Finally, we can see that calling cat with an absolute path will display the content of the file3.txt file regardless the position of the current directory.

By contrast, a relative path starts from the current working directory

$> cat another_directory/file3.txt
I'm the file #3
$> cd directory && pwd
/Users/mehdi/path/directory
$> cat ../another_directory/file3.txt
I'm the file #3

Here, we cat the file3.txt file by using a relative path.

After, we move to the directory folder.

Then we can see that calling cat with a relative path — including the ../ prefix to move to the previous directory — will display the content of the file regarding the position of the current directory.

The Kernel#require method

This method expects that the argument includes an absolute path.

Otherwise, the method tries to find the file or library by prepending it with each path included in the $LOAD_PATH global variable — or with the current working directory path if ./ prepend the argument passed to the require method.

When the file or library is loaded then the absolute path is added to the $LOADED_FEATURES global array.

The $LOADED_FEATURES global array is used to check if a file or a library (a.k.a feature) has already been required. If so, the file or library is not loaded again and the Kernel#require method returns false.

Let’s have a look to the following example to detail the behavior of this method

$> tree
.
├── hello.rb
0 directories, 1 file

So, let’s try to require hello.rb within a script in the same directory

Here we pass the ./hello.rb argument to require. Because the argument is prepended by ./ then require determines an absolute path based on the current working directory.

When the file is loaded, the determined absolute path is added to the $LOADED_FEATURES global array.

Finally, because the hello.rb file is included in the $LOADED_FEATURES array then, a second call to require './hello.rb' returns false .

Now, let’s try to require the rake gem

Here we pass the rake argument to require. In this case, require tests each absolute path included in the $LOAD_PATH global array.

If a path matches, then the library is loaded and the path is added in the $LOADED_FEATURES array.

Finally, because the rake library is included in the $LOADED_FEATURES array then, a second call to require 'rake' returns false.

the require_relative method

It first finds the requiring file or library path, and then determine an absolute path according to the required file or library.

When the file or library is loaded then the absolute path is added to the $LOADED_FEATURES.

Let’s try to require the hello.rb file by using the Kernel#require_relative method

This script runs in the same directory as our hello.rb file.

The current script path is /Users/mehdi/require/.

So the require_relative method adds the path passed as argument and determines an absolute path.

In this case, the final absolute path is /Users/mehdi/require/hello.rb.

If this path exists then the absolute path will be added to the $LOADED_FEATURES array.

Feel free to have a look to the Loading a file in Ruby article.

Voilà !

Thank you for taking the time to read this post :-)

Feel free to 👏 and share this Medium post if it has been useful for you.

Here is a link to my last medium post: Ruby: instance_eval.

RubyCademy

E-Learning platform for Ruby and Ruby on Rails