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


Groups > comp.lang.ruby > #3823

LoadPath and require confusion, please explain

From Lucky Dev <lucky.developer@gmail.com>
Newsgroups comp.lang.ruby
Subject LoadPath and require confusion, please explain
Date 2011-05-02 01:35 -0500
Organization Service de news de lacave.net
Message-ID <2f924ae07653151e6e47db274f29bc42@ruby-forum.com> (permalink)

Show all headers | View raw


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/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web