How can I require other module?

My ruby script and module script are in the same directory. I print the
$: variable and found the current directory “.” is in there. But I
always get the error: no such file to load.

What is the problem?

2008/8/28 Zhao Yi [email protected]

My ruby script and module script are in the same directory. I print the
$: variable and found the current directory “.” is in there. But I
always get the error: no such file to load.

What is the problem?

Would help if you posted your code, but I’ll hazard a guess. I usually
require files relative to the current one like this:

require File.dirname(FILE) + ‘/my_module’

The value of File.dirname(FILE) is always the path of the directory
containing the currently executing file.

In article [email protected],
Zhao Yi [email protected] wrote:

My ruby script and module script are in the same directory. I print the
$: variable and found the current directory “.” is in there. But I
always get the error: no such file to load.

Are you executing the script from the same directory? Having “.” in
RUBY_PATH only means that your “current” directory will be checked, not
the one actually hosting the script.

If you are in “/tmp” and you have “/usr/local/lib/ruby:.” in RUBY_PATH
(aka $:) then, with bar.rb/foo.rb in $HOME

require “foo”

will make Ruby looking in “/usr/local/lib/ruby” and “/tmp”, not in
$HOME.

What you want is something like the following:

BASE_DIR = File.dirname(File.expand_path($0))

$: << BASE_DIR

require “foo”

Cheers,

Hi Robert and James,

Both of your code can solve my problem, thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs