How is the default Ruby LOAD_PATH determined?

load-path, require, ruby

Solution

On my machine, the initial load path looks like this:

$ ruby -e 'puts $LOAD_PATH'
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/vendor_ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.8.0
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/vendor_ruby
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1
/Users/matt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/x86_64-darwin10.8.0

Armed with `grep`, an investigation into the Ruby source leads to the definition of `ruby_initial_load_paths[]` in `version.c` (this is on Ruby 1.9.3). The first of these that apply (neither `NO_INITIAL_LOAD_PATH` or `RUBY_SEARCH_PATH` have been set) is `RUBY_SITE_LIB2`. Looking at the defines above that definition we see:

#define RUBY_SITE_LIB2              RUBY_SITE_LIB    "/"RUBY_LIB_VERSION

and in turn:

#define RUBY_SITE_LIB RUBY_LIB_PREFIX"/site_ruby"

Following this chain of defines, it becomes clear that this corresponds to the first entry in my load path above. Similarly the other constants that go into this variable correspond to the other load path entries.

The `ruby_initial_load_paths[]` variable is used in `ruby_init_loadpath_safe()` in `ruby.c`, where the actual load path is set up for the process.

So the answer to your question is that the initial load path is set at compile time with some `#define`s, according to how the build has been configured.

Problem

Assuming I compile my own fresh Ruby (MRI 1.9.3), what is the default LOAD_PATH, and how is that computed?

Original source