12

This is actually a question about this question: Adding a directory to $LOAD_PATH (Ruby)

What happens when you add a directory to the $LOAD_PATH? Is it like adding a script file to the execution context as in JavaScript? (You can access global methods/objects in another files) If not, how do I call the methods of other ruby files in the current directory?

Community
  • 1
  • 1
RubyDosa
  • 667
  • 2
  • 7
  • 15

2 Answers2

14

When you add the /Users/you/scripts/ruby directory to the load path, you can use:

require 'example'

instead of:

require '/Users/you/scripts/ruby/example.rb'
Adrian Pacala
  • 1,011
  • 1
  • 8
  • 12
  • We still have to use `require` to include that class in our code. So, Is it okay to say that it only helps to specify the relative path instead of absolute path? – RubyDosa Mar 03 '11 at 08:11
  • wished more of the answers were this simple and straightforward. great use of example to explain a concept. kudos @adrian – berto77 Dec 29 '11 at 15:40
1

Think of the $LOAD_PATH as to being similar to the PATH variable on a operating system. If certain directories are in the LOAD_PATH, you can just write require "some_module". It's also the reason for being able to require files from the current directory.

By default, the LOAD_PATH no longer includes the current directory . having been removed in Ruby 1.9.2.

notapatch
  • 6,569
  • 6
  • 41
  • 45
Geo
  • 93,257
  • 117
  • 344
  • 520