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.

sawa
  • 165,429
  • 45
  • 277
  • 381
gugo
  • 237
  • 2
  • 7
  • 17
  • 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 '13 at 08: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. – Arup Rakshit Sep 25 '13 at 08: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 '13 at 08: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.. :) – Arup Rakshit Sep 25 '13 at 08:49
  • possible duplicate of [What is require\_relative in Ruby?](http://stackoverflow.com/questions/3672586/what-is-require-relative-in-ruby) – the Tin Man Sep 25 '13 at 10:55

2 Answers2

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'
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
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 $:.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • 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 `
    '
    – gugo Sep 25 '13 at 08:39
  • If your file system is arranged as per your description,then it should work. Because I have tested the same. – Arup Rakshit Sep 25 '13 at 08:41