LoadPath and require confusion, please explain

Guys,

I’m getting confused by requiring and the loadpath array.

By my understanding,

  1. $LOAD_PATH is an array for locations where ruby interpretor will
    search for files to load/require.
  2. Requiring or loading a thing by (require “arel” or load “arel”),
    means we are actually running the file arel.rb, found somewhere in our
    $LOAD_PATH locations.

Let me explain my doubt with some irb session code.

ruby-1.9.2-p180 :001 > puts $:
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/x86_64-darwin10.6.0
=> nil
ruby-1.9.2-p180 :002 > require ‘cgi’
=> true
ruby-1.9.2-p180 :003 > puts $:
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/x86_64-darwin10.6.0
=> nil
ruby-1.9.2-p180 :004 > require ‘arel’
=> true
ruby-1.9.2-p180 :005 > puts $:
/Users/lakshman/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/vendor_ruby
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1
/Users/lakshman/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/x86_64-darwin10.6.0
=> nil

cgi is in ruby standard library. For require “cgi”, cgi.rb is in
~/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1 and it didnt give any error
while requiring it.

arel is a gem. arel.rb is found in
~/.rvm/gems/ruby-1.9.2-p180/gems/arel-2.0.9/lib. This location was not
there in the $: and the call for require ‘arel’ didn’t throw an error.
The location was added AFTER I requried it.

whats happening for these ruby gems. can you explain how this require
and new-path-additions-to-load-path works…

When you do require/load ruby will use $GEM_PATH also to look for
available gems. in your irb check output for ENV[‘GEM_PATH’] it should
show correct location where your ‘arel’ gem is.
$LOAD_PATH comes with ruby interpreter, but $GEM_PATH is set by rvm (to
be used for gem utility).
Besides, there is also $GEM_HOME, that var also comes into play when
searching for gems.

Check your environment variables with ‘env’ bash command.


The heart is that by implicitly(*1) or explicitly requiring ‘rubygems’
library
it redefines Kernel#require to look into gem directories and modify
the $LOAD_PATH.

  • ruby 1.9 does. 1.8 with RUBYOPT -rubygems does, too.

2011/5/2 Lucky D. [email protected]:

does ruby look somewhere else apart from load_path when require/load a
feature ?


OZAWA Sakuro

“I think we can agree, the past is over.” - George W. Bush

Lucky D. wrote in post #996171:

But when i say require “something” , something.rb should be present in
the loadpath for the require call to return success…

Nope. Look, when we require arel (for example), although it is not in
$LOAD_PATH, ruby will use paths provided by gem library (as I said in
post before) to find where is arel localted.

$ ruby -e “puts require ‘arel’”
true

If has found ‘arel’ library (my load_path is almost same as your - no
arel there)

But, when we disable gems as in command below:

$ ruby -e “puts require ‘arel’” --disable-gems
-e:1:in require': no such file to load -- arel (LoadError) from -e:1:in

Ruby fails to find arel library, because it ignores $GEM_HOME and
$GEM_PATH this time, and search only in $LOAD_PATH.

‘–disable-gems’ option is your answer.


But when i say require “something” , something.rb should be present in
the loadpath for the require call to return success…

here for arel, require is returning “true” even when its not in the
load_path and after i call require, the appropriate path is added to
load_path.

does ruby look somewhere else apart from load_path when require/load a
feature ?

Ruby fails to find arel library, because it ignores $GEM_HOME and
$GEM_PATH this time, and search only in $LOAD_PATH.

‘–disable-gems’ option is your answer.


http://blog.eugen.co

thanks :slight_smile: got it now … :slight_smile: