Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3823 > unrolled thread

LoadPath and require confusion, please explain

Started byLucky Dev <lucky.developer@gmail.com>
First post2011-05-02 01:35 -0500
Last post2011-05-03 11:37 -0500
Articles 6 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  LoadPath and require confusion, please explain Lucky Dev <lucky.developer@gmail.com> - 2011-05-02 01:35 -0500
    Re: LoadPath and require confusion, please explain Eugen Ciur <ciur.eugen@gmail.com> - 2011-05-02 02:26 -0500
    Re: LoadPath and require confusion, please explain Lucky Dev <lucky.developer@gmail.com> - 2011-05-02 04:15 -0500
      Re: LoadPath and require confusion, please explain OZAWA Sakuro <sakuro@2238club.org> - 2011-05-02 04:22 -0500
      Re: LoadPath and require confusion, please explain Eugen Ciur <ciur.eugen@gmail.com> - 2011-05-02 04:40 -0500
        Re: LoadPath and require confusion, please explain Lucky Dev <lucky.developer@gmail.com> - 2011-05-03 11:37 -0500

#3823 — LoadPath and require confusion, please explain

FromLucky Dev <lucky.developer@gmail.com>
Date2011-05-02 01:35 -0500
SubjectLoadPath and require confusion, please explain
Message-ID<2f924ae07653151e6e47db274f29bc42@ruby-forum.com>
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.....

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#3824

FromEugen Ciur <ciur.eugen@gmail.com>
Date2011-05-02 02:26 -0500
Message-ID<425eb8d7f3f4a4e18dc557b982a2000c@ruby-forum.com>
In reply to#3823
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.

----
http://blog.eugen.co

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3827

FromLucky Dev <lucky.developer@gmail.com>
Date2011-05-02 04:15 -0500
Message-ID<ea962f54b488388ca9cea05b10caf333@ruby-forum.com>
In reply to#3823
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 ?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3828

FromOZAWA Sakuro <sakuro@2238club.org>
Date2011-05-02 04:22 -0500
Message-ID<BANLkTinC2vR=y2GcdihzXUWvZg8MPGU7VA@mail.gmail.com>
In reply to#3827
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 Dev <lucky.developer@gmail.com>:
> 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

[toc] | [prev] | [next] | [standalone]


#3830

FromEugen Ciur <ciur.eugen@gmail.com>
Date2011-05-02 04:40 -0500
Message-ID<20f2c407df4415d42c965c8e8f251470@ruby-forum.com>
In reply to#3827
Lucky Dev 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 `<main>'

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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3880

FromLucky Dev <lucky.developer@gmail.com>
Date2011-05-03 11:37 -0500
Message-ID<876b76b1b3392f1889bbe1f717989d68@ruby-forum.com>
In reply to#3830
> 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 :) got it now .. :)

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web