breaking into the unknown…

require and load in ruby : LoadError cannot load such file


require is used to call one file into another file. The detail about require is available here and about load is  available here on ruby doc. There purpose is same, but are different in approach. I will explain the concept in terms of require, which hold true for load also. Towards the end I will list the difference between the two.

Let I have a ruby file myprogram.rb in  Document/projects folder. I need to call the method contained in it from irb. We know that to access any method within a file or library we need to require or load that file/library in the current scope (irb in this case). So, I will try to require myprogram.rb file.

$ irb
1.9.3-p194 :001 > require "myprogram"
LoadError: cannot load such file -- myprogram

Its not working, you may think that we have not provided the .rb extension, that’s why it is not loading. Let us try again.

1.9.3-p194 :001 > require "myprogram.rb"
LoadError: cannot load such file -- myprogram

Still the same error, So absence of extension is not a problem . Basically ruby is smart enough to add the extension by own. If the filename has the extension “.rb”, it is loaded as a source file; if the extension is “.so”, “.o”, or “.dll”, or the default shared library extension on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding “.rb”, “.so”, and so on to the name until found. If the file named cannot be found, a LoadError will be raised.

But, why it is happening. We have seen this syntax working many time in case of gems and files placed in lib folder of a Rails app.

The reason is that ruby directly search for the file to require in all the directory listed in $LOAD_PATH. You can find the directory included in ruby search path, by typing $LOAD_APTH or simply $:

1.9.3-p194 :005 > $LOAD_PATH
 => ["/home/arun/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1",

So, ruby will search the file you are specifying in above folder. You can see that the folder Document/projects is not listed in load path so the require syntax we are using is not working.

NOTE : ruby first try to find a file by its absolute path. If ruby do not able to find the file by its absolute path, it try to find it in all the directory listed in $LOAD_PATH. If it is not found there also, It will raise LoadError: cannot load such file

So we can use any of the below solution.

SOLUTION 1: specify the absolute path of the file you want to require.

1.9.3-p194 :001 > require "Document/projects/myprogram.rb"
LoadError: cannot load such file -- myprogram

Again, it is not working. this is because you have specified the absolute path wrongly. It should be start from your home directory. below will work.

1.9.3-p194 :001 > require "/home/arun/Document/projects/myprogram.rb"
=> true

So, now it worked. But specifying absolute path is tedious. The second solution provide a better approach.

SOLUTION 2: Add the folder containing your program to the ruby $LOAD_PATH.
If you are working in any RAILS application you can specify it in config/application.rb file. It always include lib folder by default. That is the reason why, you just need to specify the file name with require if it is placed in lib folder, not the absolute path. The default line look like this.

Auto Loading custom folder in rails application

config.autoload_paths += %W(#{config.root}/lib)

So this line will add lib folder of your application to the ruby $LOAD_PATH. let us modify the above line to include, say extra_program folder in the load path.

config.autoload_paths += %W(#{config.root}/lib #{config.root}/extra_program)

So any file in extra_program folder of your rails app will also be added to auto load path.

Outside Rails app, like in above case where the file is located in /home/arunDocument/projects folder, we can add it to $LOAD_PATH as below from the irb or rails console.

1.9.3p194 :001 > $LOAD_PATH.unshift File.expand_path('/home/arunDocument/projects', __FILE__)
 => ["/home/arunDocument/projects", 
1.9.3p194 :002 > require "myprogram"
 => true

So we can see that /home/arunDocument/projects added to auto load path and the simple require syntax is working now.

load can be used exactly as require, but have below differences:


1=> File Extension need to be specified with load but not with require

1.9.3p194 :002 > require "myprogram.rb" # work without extension
 => true 
1.9.3p194 :003 > load "myprogram" # load do not work without extension
LoadError: cannot load such file -- ruby_block
	from (irb):3:in `load'
	from (irb):3
	from /home/arun/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `'
1.9.3p194 :004 > load "myprogram.rb" # working with extension
 => true

2=> in any current scope, require compile the file only first time it is encountered, load do it every time.

say we are working in irb, so our current scope is irb. So on irb, if you require the myprogram.rb once then you make some changes and again require the file, your changes will not reflect, as it is not compiled again. To make your changes reflect, you need to exit irb and restart again to make require to compile your new changes. But load do it every time it is encountered. So if any file content keep changing with time, you should better use load instead of require.



Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

5 thoughts on “require and load in ruby : LoadError cannot load such file

  1. Thank you. Very helpful

  2. Thank you.
    $LOAD_PATH.unshift File.expand_path(‘/home/arunDocument/projects’, __FILE__) solve my problem

  3. Very useful blog. Well explained.

  4. i want to load .ini file in my .rb file

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s