Wednesday, February 6, 2008

Require vs Load

In ruby there are two main ways to include additional source files into working enviroment. One way is with the Kernel#load method and the other is with the Kernel#require method.

So what are the main difference and when should one be used over the other?

Some simple examples should clear up any confusion.

Say we have a very useful file called 'useful.rb' that has the following contents.

puts "This file is so useful I just want to use it everywhere!"


Now lets play in our nifty IRB and 'require' our destined to be hugely popular file.

>> require 'useful'
This file is so useful I just want to use it everywhere!
=> true


Ahh all is well in the world. Now lets try to load it.

>> load 'useful'
LoadError: no such file to load -- useful
from (irb):2:in 'load'
from (irb):2
>>


As you can see 'load' was unable to locate the file. Lets try adding the .rb to extension and see what happens.

>> load 'useful.rb'
This file is so useful I just want to use it everywhere!
=> true


There we go. So the first difference I want to point out is that 'load' requires that you explicitly add the file extension while 'require' does not. 'require' will automatically search your path for files with the given name and automatically append .rb, .so, .o and .dll extensions to try to locate the proper file.

Now lets see what happens if we 'require' and 'include' our file a second time.

>> require 'useful'
=> false
>> load 'useful.rb'
This file is so useful I just want to use it everywhere!
=> true


As you an see requiring the file a second time fails but including it passes. This is because when you 'require' a file it will not reload itself if it has already been loaded. This is in contrast to 'load' will always reload the file.

Why would you ever use 'load' then? Well the truth is most of the time you will be using 'require' but there are circumstances where 'load' is preferably. Rails for example uses 'load' while in development mode as to guarantee any changes to the source will invalidate any existing cache that the web server may have thus always displaying your most recent changes.

I hope this post clears up any confusion.

14 comments:

ToddCS said...

Hello, Robert -

You concluded your piece with "I hope this post clears up any confusion."

Well, sort of. But not really.

I am sure you are a consummate professional in your field. But your English is about a sloppy as any I have ever seen. You wrote:

>> but there are
>> circumstances where 'load'
>> is preferably.

Ouch. Did you mean "preferable", perhaps?

And your (non-)use of punctuation:

You wrote:

>> "Now lets play in our
>> nifty IRB
>> and 'require' our destined
>> to be
>> hugely popular file."

I had to read that sentence about four times until I finally figured out that you meant: "... our destined-to-be-hugely-popular file."

And by the way: You should have written "Let's", not "Lets". As you used the word, it is a contraction for "let us", and a contraction requires an apostrophe.

Worst of all, the purported premise of your post is to explain the difference between 'load' and 'require' in Ruby. But then you start mentioning 'include' out of left field, while you apparently meant 'load', not 'include'. You wrote:

>> "Now lets see what happens
>> if we 'require'
>> and 'include' our file a
>> second time.
>> ...
>> As you an see requiring
>> the file a
>> second time fails but including >>it passes."

Also, the opposite of "fail" in this context is "succeed", not "pass". You can fail or pass a test, but the test itself fails or succeeds.

So, again, this is not meant as a personal attack. On the contrary, as your fellow human being and software developer I assure you that I hold you in the highest possible regard. But if you want to help your colleagues with your writing, please write better and clearer.

Thanks!

Todd

Unknown said...

Thanks Robert - that answered my query.

And err, ToddCS, if you read this page ever again... lighten up - it really doesn't matter! And it's not like your post is perfect either ;)

Anonymous said...

ttp://www.radiumforums.com/showthread.php?t=29090 Buy VIAGRA only $0.84. CIALIS only $1.45

Unknown said...

@ToddCS

Wow. Your snarky, pedantic comment adds absolutely nothing to the content of this post. Perhaps rather than spending so much effort correcting someone (who, I might add, you do not know, and may not even be a native English speaker) you should consider adding something *useful*

mooman said...

Hey, "ToddCS" is spelled wrong. It should probably be spelled as "douche bag".

Xavier Nayrac said...

Thank you for this post. I play with mod_ruby today. Now I understand why I need to use load instead of require when developping.

Dave Sims said...

ToddCS wrote:

"But your English is about a sloppy as any I have ever seen. You wrote:"

I'm just pointing out that ToddCS wrote:

"But your English is about a sloppy as any I have ever seen. You wrote:"

Is ToddCS's English about a sloppy as I have ever seen? I don't know. I've seen a lot of English. Seriously, I go out every day, and let me tell you, I see English. But of all the English I've seen ToddCS's is about a sloppy as I've ever seen.

Anonymous said...

Thanks a lot..this clears my doubt.

Anonymous said...

Nice blog. Thanks!

In the following sentence: "As you an see requiring the file a second time fails but 'including' it passes."

Do you mean "loading" instead of "including"?

Anonymous said...

ToddCS => TROLL!

Good post. A bit too informal for me. Guess I better check the code.

vegitalander said...

@everyone replying to ToddCS

STOP FEEDING THE TROLL!!!

webmites said...

Thanks, this cleared the confusion for me. The behavior of load is pretty weird though.

Anonymous said...

Feedback is a gift.

bparanj said...

Very simple and clear. Thanks.