# How can I require other module?

**URL:** <https://www.ruby-forum.com/t/how-can-i-require-other-module/147115>\
**Category:** Ruby\
**Created:** [August 28, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/how-can-i-require-other-module/147115 "2008-08-28T15:26:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 28, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/how-can-i-require-other-module/147115/1 "2008-08-28T15:26:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 28, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/how-can-i-require-other-module/147115/2 "2008-08-28T15:42:31Z")

</div>

2008/8/28 Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 28, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/how-can-i-require-other-module/147115/3 "2008-08-28T15:46:03Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Zhao Yi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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,

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [August 28, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/how-can-i-require-other-module/147115/4 "2008-08-28T16:28:45Z")

</div>

Hi Robert and James,

Both of your code can solve my problem, thanks.
