0

I have a directory tree.

- app.rb
- folder/
  -one.rb

app.rb

 $:.unshift File.dirname(__FILE__)
 require 'folder/one'

When I ran ruby app.rb, I got this error:

`require': cannot load such file -- ./radius/dictionary (LoadError).

I don't know why. Please help.

5
  • 1
    You are requiring folder/one and you get an error about loading ./radius/dictionary, maybe there is something missing in you example?
    – toro2k
    Sep 25, 2013 at 8:30
  • @toro2k I think for simplicity OP gave some conceptual names...according to his/her FS... But forgot to edit the actual error message according to the same. Sep 25, 2013 at 8:39
  • @ArupRakshit Probably not, there is no problem with OP code as it is posted, my guess is that the error comes from one.rb, but without seeing the file or the complete stack trace is quite hard to tell.
    – toro2k
    Sep 25, 2013 at 8:45
  • @toro2k Yes there is a problem... Did you read my answer? OP added the file path for app.rb.Not the one for one.rb. Accordingly I tested. Either you or me missing something.. :) Sep 25, 2013 at 8:49
  • possible duplicate of What is require_relative in Ruby? Sep 25, 2013 at 10:55

2 Answers 2

3

When the location of the file you are loading is relative to the file you are loading it from, use require_relative:

require_relative 'folder/one'
0

Try only the below:

 $:.unshift(File.dirname(__FILE__) + '/folder')
 require 'one'

The above will work.

Your one is not working as,you are adding the directory of the file app.rb as path/to/file.But one.rb is inside the path/to/file/folder/one.rb. So you need to add path/to/file/folder in $:.

2
  • Hi, I tried yours, then I got this error: /Users/min/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in require': cannot load such file -- one (LoadError) from /Users/min/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in require' from test.rb:3:in `<main>'
    – gugo
    Sep 25, 2013 at 8:39
  • If your file system is arranged as per your description,then it should work. Because I have tested the same. Sep 25, 2013 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.