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?

···

--
Posted via http://www.ruby-forum.com/.

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.

···

2008/8/28 Zhao Yi <youhaodeyi@gmail.com>

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?

--
James Coglan
http://blog.jcoglan.com

In article <21639a804e467bb3f36669aab5b135fd@ruby-forum.com>,

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,

···

Zhao Yi <youhaodeyi@gmail.com> wrote:
--
Ollivier ROBERT -=- EEC/RIF/SEU -=-
Systems Engineering Unit

Hi Robert and James,

Both of your code can solve my problem, thanks.

···

--
Posted via http://www.ruby-forum.com/.