# Using 'require' with modules in the same folder

**URL:** <https://www.ruby-forum.com/t/using-require-with-modules-in-the-same-folder/119369>\
**Category:** Ruby\
**Created:** [November 6, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/using-require-with-modules-in-the-same-folder/119369 "2007-11-06T13:23:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/using-require-with-modules-in-the-same-folder/119369/1 "2007-11-06T13:23:37Z")

</div>

Hi people,

I’ve modularized my very first ruby program (a port of a bash script).

However, if I use ‘load’ and the target program is loaded multiple times  
(indirectly), I get warnings about redefinition of constants, etc  
(understandably)

But if I try to use ‘require’, it searches for the program in the ruby  
distribution. Is there a way to specify within the top level script  
itself  
where to search?

TIA

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/using-require-with-modules-in-the-same-folder/119369/2 "2007-11-06T14:07:35Z")

</div>

Fernando C. wrote:

> itself where to search?  
> You can use relative/absolute paths with require. Combined with  
> **FILE** , more precisely File.dirname( **FILE** ), you can require other  
> files relative to your current one.

mortee

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/using-require-with-modules-in-the-same-folder/119369/3 "2007-11-06T14:09:20Z")

</div>

Fernando C. wrote:

> But if I try to use ‘require’, it searches for the program in the ruby  
> distribution.  
> Never mind… the current folder is included in the search path, I just  
> got  
> confused by an error message from the loaded program.

> Is there a way to specify within the top level script  
> itself where to search?

FWIW

$: \<\< “additional\_path”

seems to do it, according to “Programming ruby”

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 2:14pm UTC](https://www.ruby-forum.com/t/using-require-with-modules-in-the-same-folder/119369/4 "2007-11-06T14:14:36Z")

</div>

On Nov 6, 2007, at 1:00 PM, Fernando C. wrote:

> script itself where to search?  
> Yes, there’s a standard idiom for that:

$:.unshift(File.dirname( **FILE** ))

which means: prepend to the list of directories where libraries are  
searched ($:) the directory where this file lives. Note that does not  
assume the directory is the current working directory, since **FILE**  
points to the containing file, no matter how is being evaled.

– fxn
