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

**URL:** <https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128>\
**Category:** Ruby\
**Created:** [October 7, 2010, 7:19am UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128 "2010-10-07T07:19:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [October 7, 2010, 7:19am UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128/1 "2010-10-07T07:19:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [October 7, 2010, 10:04am UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128/2 "2010-10-07T10:04:39Z")

</div>

On Thu, Oct 7, 2010 at 8:19 AM, Tom W.rop [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [October 7, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128/3 "2010-10-07T12:37:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [October 7, 2010, 2:11pm UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128/4 "2010-10-07T14:11:42Z")

</div>

On Thu, Oct 7, 2010 at 1:37 PM, Tom W.rop [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [October 7, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128/5 "2010-10-07T17:33:44Z")

</div>

-----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/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJMrebmAAoJEGrS0YjAWTKV9rMH/344yk7tH33uImJGLXO8hyEG  
DviFgp626Yc4hy5ZH+CrQG+gkpUcGPlIz5t4L7xLp5Jh7kO3ob1nwcNiCxUnTckh  
81R/LYCYZaUI5LXPP+2OTad99VWnFSAgXaZGUjQVrzBAM9VjgogyVBrX0n7UdvVp  
l6cArRCll4O6YzragH5x9hPWcSFztRQIG4Eeic6Mxd5fXEedLj5S2y32bcOvRMXm  
5cJHHEmSZF9LxndOZqLjK70XafwMJWueTXL7ugQRKH/U/yp4/IR2ktp708MBPxgO  
5QgFeKzK8XCqbW2X4uM4a2tw66y9A+QEvobHIKiBNXU2yDy8bqcUHDGXZZ66pM4=  
=Y9+s  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![tomfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@tomfarm](https://www.ruby-forum.com/u/tomfarm)\
**Post date:** [October 8, 2010, 6:23pm UTC](https://www.ruby-forum.com/t/load-works-but-require-doesnt/196128/6 "2010-10-08T18:23:52Z")

</div>

On Thu, Oct 7, 2010 at 6:28 PM, Quintus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
