Ruby - Inclusions from Other Directories


Both load and require can take local or absolute filenames.

For example, require 'a' first looks for a.rb in the current directory.

And then iterates through a multitude of other directories on your hard drive looking for a.rb.

Ruby stores the list of directories to search for included files in a special variable called $ or $LOAD_PATH.

You can see what $: contains by default


$:.each { |d| puts d }


To add extra directories to the search path:

$:.push '/your/directory/here'  
require 'yourfile' 

$: is an array, and you can push extra items to it or use unshift to add an element to the start of the list if you want your directory to be searched before the default Ruby ones to override Ruby's standard libraries.

Related Topic