views:

72

answers:

2

Is there any major difference between load and require in the Ruby on Rails applications? Or do they both have the same functionality?

+2  A: 

require searches for the library in all the defined search paths and also appends .rb or .so to the file name you enter. It also makes sure that a library is only included once. So if your application requires library A and B and library B requries library A too A would be loaded only once.

With load you need to add the full name of the library and it gets loaded every time you call load - even if it already is in memory.

Nikolaus Gradwohl
Thank you sir , so we can say .rb is not require in loading . And "require" does not call all times , if its in memory?
Arpit Vaishnav
yes require calls only once per session
Nikolaus Gradwohl
A: 

Another difference between Kernel#require and Kernel#load is that Kernel#load takes an optional second argument that allows you to wrap the loaded code into an anonymous empty module.

Jörg W Mittag