[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How can I require other module?

Zhao Yi

8/28/2008 1:22:00 PM

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-....

3 Answers

James Coglan

8/28/2008 1:36:00 PM

0

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

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?




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.

--
James Coglan
http://blog.j...
http://github.c...

Ollivier Robert

8/28/2008 1:44:00 PM

0

In article <21639a804e467bb3f36669aab5b135fd@ruby-forum.com>,
Zhao Yi <youhaodeyi@gmail.com> 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,
--
Ollivier ROBERT -=- EEC/RIF/SEU -=-
Systems Engineering Unit

Zhao Yi

8/28/2008 2:25:00 PM

0

Hi Robert and James,

Both of your code can solve my problem, thanks.

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