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

T

Tom Wardrop

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
 
A

Ammar Ali

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
 
T

Tom Wardrop

Ammar said:
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!
 
A

Ammar Ali

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
 
Q

Quintus

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top