views:

37

answers:

2

Whenever I require a file in ruby or irb I get this error:

LoadError: no such file to load -- (insert any filename).rb
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from (irb):1
    from /usr/bin/irb1.9.1:12:in `<main>'

It happens even if the file exists I am using ruby1.9.1 and to my knowledge, I have not installed rubygems. I am running on Ubuntu 10.10 Maverick Meerkat. Please help, this problem is very annoying! Thanks in advance, ell.

EDIT: I forgot to say that no matter where the file is, even if its in the same directory and definately exists I always get this error.

+1  A: 

Rubygems is installed with ruby 1.9 by default.

Check that the file you are trying to load is in a directory listed in the variable $: or specify the entire path to the file in the require. Or, add the directory to $: explicitly:

$: << '/my/lib/path'
require 'mylib'
SteveRawlinson
Thanks, this fixed the problem. Sorry for being a noob, I have only just moved over to Ubuntu from Windows XP and it takes a bit of getting used to, especially the package install system!
Ell
+2  A: 

In Ruby 1.9.2, which I guess is the version you are using, the current directory is no longer in the $LOAD_PATH. If you want to require files relative to the path of the file that the require call is in, you should use require_relative instead.

If you really want to require files relative to the current directory, then you can add the current directory to the $LOAD_PATH like so:

$LOAD_PATH << '.'

However, this change was made for a reason, so you shouldn't do that lightly. After all, this will make your app behave more or less randomly, depending on what directory you just happened to be in, when you started the app. Worse, an attacker can get you to execute arbitrary code on his behalf if he can get you to run the app from a directory under his control.

Jörg W Mittag