# Load vs require

**URL:** <https://www.ruby-forum.com/t/load-vs-require/150373>\
**Category:** Ruby\
**Created:** [October 7, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/load-vs-require/150373 "2008-10-07T21:32:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/load-vs-require/150373/1 "2008-10-07T21:32:24Z")

</div>

I started looking through the source code for rake and noticed something  
curious.

I see that when I execute the “rake” command at the command prompt, I  
run a  
fairly trivial ruby script:

require ‘rubygems’

version = “\>= 0”

if ARGV.first =~ /^_(.\*)_$/ and Gem::Version.correct? $1 then  
version = $1  
ARGV.shift  
end

gem ‘rake’, version  
load ‘rake’

The #gem method prepended “…/gems/rake-0.8.3/bin” and  
“…/gems/rake-0.8.3/lib” to my $LOAD\_PATH (the “bin” directory first).  
Thus, when the script invokes the #load method, it grabs  
“…/gems/rake-0.8.3/bin/rake” and starts to execute it. It looks like  
this:

begin  
require ‘rake’  
rescue LoadError  
require ‘rubygems’  
require ‘rake’  
end  
Rake.application.run

Somehow, the “#require” method knows that it should load the “rake.rb”  
file  
from the “lib” directory (the second element in $LOAD\_PATH) instead of  
the  
already loaded “rake” from the “bin” directory, (the fist element in  
$LOAD\_PATH).

Why is that?

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 7, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/load-vs-require/150373/2 "2008-10-07T21:52:58Z")

</div>

Alle Tuesday 07 October 2008, Patrick D. ha scritto:

> if ARGV.first =~ /^_(.\*)_$/ and Gem::Version.correct? $1 then  
> “…/gems/rake-0.8.3/bin/rake” and starts to execute it. It looks like  
> Somehow, the “#require” method knows that it should load the “rake.rb” file  
> from the “lib” directory (the second element in $LOAD\_PATH) instead of the  
> already loaded “rake” from the “bin” directory, (the fist element in  
> $LOAD\_PATH).
> 
> Why is that?
> 
> –wpd

According to the ri documentation for require, if you pass it a filename  
which  
doesn’t have extension .rb or .so (or .dll or .o, depending on your  
system),  
then require tries to add those extensions to the file name and to load  
the  
corresponding library. So, the code

require ‘rake’

will cause ruby to look for one of the files rake.rb or rake.so in the  
directories listed in $:. The script in bin is called simply rake, not  
rake.rb, and so require doesn’t even consider it.

load, instead, interpret its argument in a different way: it considers  
it to  
be the whole basename of the file and doesn’t try to add any extension  
to it.  
So, the line

load ‘rake’

will make ruby look for a file called ‘rake’ (not rake.rb as before) in  
the  
directories listed in $:. This time bin/rake has the correct name and is  
loaded.

You can see it yourself by doing the following:

- create a file with a name which doesn’t end in .rb, for example  
test\_load\_require, then put some ruby code in it. Start irb in the same  
directory of the file and issue the command

require ‘test\_load\_require’

ruby will complain (raising a LoadError) because it can’t find the file.  
Now  
call

load ‘test\_load\_require’

and you’ll see that the file is correctly loaded and the code in it is  
executed.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [October 8, 2008, 2:47am UTC](https://www.ruby-forum.com/t/load-vs-require/150373/3 "2008-10-08T02:47:16Z")

</div>

Ahhh… I read “Ruby tries adding `.rb'',`.so’’, and so on to the  
name”  
in the documentation for #require over and over and it never occurred to  
me  
that Ruby might not try loading the named file unadorned with an  
extension.

Thank you.

–wpd
