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.