"Load" Works, But "Require" Doesn't?

Discussion in 'Ruby' started by Tom Wardrop, Oct 7, 2010.

  1. Tom Wardrop

    Tom Wardrop Guest

    Can someone please explain what's going on here. No matter what I try,
    every time I call require() with a relative path, it comes back with
    "LoadError: no such file to load". Yet, at the same time, load() works
    perfectly fine with the exact same relative path. Require() doesn't seem
    to have a problem with absolute paths, in fact this bit of code works
    flawlessly...

    require File.expand_path('subfolder/file.rb')

    ...but the follow code fails with the LoadError mentioned earlier...

    require 'subfolder/file.rb'

    I've got RubyGem's installed, so I thought maybe it's override of the
    require() method was causing havoc, so I tried gem_original_require()
    with the exact same result, LoadError.

    I'm getting this problem from both the "irb" and the "ruby" executables.
    Checking Dir.pwd reveals that indeed, the current directory is as
    expected/intended.

    Can someone explain to me why require() isn't working with relative
    paths? I'm completely baffled and don't know where to go from here.

    Cheers
    --
    Posted via http://www.ruby-forum.com/.
    Tom Wardrop, Oct 7, 2010
    #1
    1. Advertising

  2. Tom Wardrop

    Ammar Ali Guest

    On Thu, Oct 7, 2010 at 8:19 AM, Tom Wardrop <> wrote:
    > Can someone explain to me why require() isn't working with relative
    > paths? I'm completely baffled and don't know where to go from here.


    What version of ruby are you using?

    In 1.9 the current directory (.) is no longer included in the load
    path ($LOAD_PATH, or $:)

    Regards,
    Ammar
    Ammar Ali, Oct 7, 2010
    #2
    1. Advertising

  3. Tom Wardrop

    Tom Wardrop Guest

    Ammar Ali wrote:
    > What version of ruby are you using?
    >
    > In 1.9 the current directory (.) is no longer included in the load
    > path ($LOAD_PATH, or $:)
    >
    > Regards,
    > Ammar


    I'm on 1.9.2. This now raises a number of questions...

    1) How come the load() method isn't affected by this?
    2) What is the logic behind not allowing files to be referenced with a
    relative path?
    3) What is the correct way for referencing files relatively?

    Thanks for your help thus far!

    --
    Posted via http://www.ruby-forum.com/.
    Tom Wardrop, Oct 7, 2010
    #3
  4. Tom Wardrop

    Ammar Ali Guest

    On Thu, Oct 7, 2010 at 1:37 PM, Tom Wardrop <> wrote:
    > I'm on 1.9.2. This now raises a number of questions...
    >
    > 1) How come the load() method isn't affected by this?


    I'm not sure, but I guess (hope someone else will chime in) it is
    because load needs a full file name, while require tries different
    extensions (.rb, .o, .so, .dl, .dyld, maybe others) potentially
    loading something you did not intend.


    > 2) What is the logic behind not allowing files to be referenced with a
    > relative path?


    Only via require, as you discovered. It's a security measure. Please
    see: http://redmine.ruby-lang.org/issues/show/1733


    > 3) What is the correct way for referencing files relatively?


    The link above mentions two possible ways which are useful on the
    command line or from scripts (-I and RUBYLIB). You can also use
    __FILE__ in combination File.expand_path to require files relatively
    from a given file:

    require File.expand_path( 'some/file', __FILE__)


    Regards,
    Ammar
    Ammar Ali, Oct 7, 2010
    #4
  5. Tom Wardrop

    Quintus Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Am 07.10.2010 14:11, schrieb Ammar Ali:
    > The link above mentions two possible ways which are useful on the
    > command line or from scripts (-I and RUBYLIB). You can also use
    > __FILE__ in combination File.expand_path to require files relatively
    > from a given file:
    >
    > require File.expand_path( 'some/file', __FILE__)
    >


    In 1.9.x, there's require_relative for that purpose.

    Vale,
    Marvin

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQEcBAEBAgAGBQJMrebmAAoJEGrS0YjAWTKV9rMH/344yk7tH33uImJGLXO8hyEG
    DviFgp626Yc4hy5ZH+CrQG+gkpUcGPlIz5t4L7xLp5Jh7kO3ob1nwcNiCxUnTckh
    81R/LYCYZaUI5LXPP+2OTad99VWnFSAgXaZGUjQVrzBAM9VjgogyVBrX0n7UdvVp
    l6cArRCll4O6YzragH5x9hPWcSFztRQIG4Eeic6Mxd5fXEedLj5S2y32bcOvRMXm
    5cJHHEmSZF9LxndOZqLjK70XafwMJWueTXL7ugQRKH/U/yp4/IR2ktp708MBPxgO
    5QgFeKzK8XCqbW2X4uM4a2tw66y9A+QEvobHIKiBNXU2yDy8bqcUHDGXZZ66pM4=
    =Y9+s
    -----END PGP SIGNATURE-----
    Quintus, Oct 7, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Hammond
    Replies:
    2
    Views:
    4,504
    bruce barker
    Nov 14, 2003
  2. F. GEIGER
    Replies:
    3
    Views:
    754
    F. GEIGER
    Aug 6, 2004
  3. Bob Hutchison
    Replies:
    0
    Views:
    165
    Bob Hutchison
    Dec 10, 2007
  4. SM
    Replies:
    5
    Views:
    115
    Martin Honnen
    May 23, 2007
  5. ChronoFish
    Replies:
    2
    Views:
    94
    ChronoFish
    Oct 22, 2007
Loading...

Share This Page