How can I require other module?

Z

Zhao Yi

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?
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

2008/8/28 Zhao Yi said:
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.
 
O

Ollivier Robert

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top