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

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

On Thu, Oct 7, 2010 at 8:19 AM, Tom W.rop [email protected] 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 A. 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!

On Thu, Oct 7, 2010 at 1:37 PM, Tom W.rop [email protected] 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.

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

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

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

Am 07.10.2010 14:11, schrieb Ammar A.:

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

On Thu, Oct 7, 2010 at 6:28 PM, Quintus [email protected] wrote:

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

I keep forgetting about that one. Probably from avoiding it enough
times for code that needs to run on 1.8.

Regards,
Ammar