65

In Ruby, I have been told that when doing

require "some_file"

Ruby will look for the file in certain places.

I know that it looks for some_file.rb, but where does it look for it by default?

Raj
  • 22,346
  • 14
  • 99
  • 142
Mark Provan
  • 1,041
  • 2
  • 13
  • 19

6 Answers6

82

It depends on your platform, and how Ruby was compiled, so there is no "the" answer to this. You can find out by running:

ruby -e 'puts $:'

Generally, though, you have the standard, site, and vendor Ruby library paths, including an arch, version, and general directory under each.

Daniel Pittman
  • 16,733
  • 4
  • 41
  • 34
  • 5
    There is a "the" answer. The $: or $LOAD_PATH variable does indeed give the full list of places that are searched. You yourself noted a simple and clean way to print that out... – Perry Feb 28 '12 at 00:04
  • 2
    Ah. Depends how you read the question: there is "the" answer to where this Ruby looks, but it will change if you run a different Ruby or on a different platform. eg: MRI 1.8.7 and REE would use different paths, or Darwin and Linux MRI use subtly different paths. I wasn't sure which, so felt more comfortable with this answer. – Daniel Pittman Feb 28 '12 at 00:07
  • 2
    That is true enough, though I'm guessing the questioner simply wanted to know how to find out what the load path was rather than assuming it was constant across platforms. – Perry Feb 28 '12 at 00:13
  • 1
    You might well be right about that. It seemed more complex the first time I read it. :) – Daniel Pittman Feb 28 '12 at 00:15
31

Ruby looks in all the paths specified in the $LOAD_PATH array.

You can also add a directory to search like so:

$LOAD_PATH.unshift File.expand_path('../path/from/this/file/to/another/directory', __FILE__)
Brian Hempel
  • 8,844
  • 2
  • 24
  • 19
11

additional paths can be specified by setting RUBYLIB environment variable

mighq
  • 1,004
  • 1
  • 13
  • 14
7

The $LOAD_PATH global variable (also named $:) contains the list of directories that are searched.

See: http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require

Perry
  • 4,363
  • 1
  • 17
  • 20
2

When calling ruby on command line, you can provide additional search paths using the -I argument. Compare the output of

$ ruby -e 'puts $:'

with the output of

$ ruby -I /tmp -e 'puts $:'

note how the second one lists /tmp as an option. You can use multiple -I to add multiple path.

You can also use it with the shebang:

#!/usr/bin/ruby -I /tmp -I /usr/local/lib/ruby
Mecki
  • 125,244
  • 33
  • 244
  • 253
1

require(string) => true or false

Ruby tries to load the library named string, returning true if successful. If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:. If the file has the extension ".rb", it is loaded as a source file; if the extension is ".so", ".o", or ".dll", or whatever the default shared library extension is on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding ".rb", ".so", and so on to the name. The name of the loaded feature is added to the array in $:.

ericraio
  • 1,469
  • 14
  • 35