# Require and relative paths

**URL:** <https://rubytalk.org/t/require-and-relative-paths/20056>\
**Category:** ruby-talk\
**Created:** [10 August 2005 15:56 UTC](https://rubytalk.org/t/require-and-relative-paths/20056 "2005-08-10T15:56:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![francisrammeloo](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@francisrammeloo](https://rubytalk.org/u/francisrammeloo)\
**Post date:** [10 August 2005 15:56 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/1 "2005-08-10T15:56:09Z")

</div>

Hi all,

Suppose I have following code:

require "./Classes/Printer" # My personal printer class

Now if I want to run my program Ruby needs to find the Printer.rb file  
in the Classes directory. But if the script was run like this:

> cd ~  
> ruby ~/rubydev/projects/texteditor/main.rb

Then the current path will be ~ and Ruby won't find  
../Classes/Printer.rb  
How do I solve this problem?

Greetings,  
Francis

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [10 August 2005 16:01 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/2 "2005-08-10T16:01:10Z")

</div>

francisrammeloo@hotmail.com wrote:

> Hi all,
> 
> Suppose I have following code:
> 
> require "./Classes/Printer" # My personal printer class
> 
> Now if I want to run my program Ruby needs to find the Printer.rb file  
> in the Classes directory. But if the script was run like this:
> 
> > cd ~  
> > ruby ~/rubydev/projects/texteditor/main.rb
> 
> Then the current path will be ~ and Ruby won't find  
> ./Classes/Printer.rb  
> How do I solve this problem?

Did you try removing "./"? Other than that, if directory "Classes" is  
always relative to the script, you can use "require  
File.join(File.basename($0), "Classes", "Printer")". Btw, I think you  
don't need the extension, Ruby will figure automatically.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Jim\_Menard3](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jim\_Menard3](https://rubytalk.org/u/Jim_Menard3)\
**Post date:** [10 August 2005 16:04 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/3 "2005-08-10T16:04:13Z")

</div>

francisrammeloo@hotmail.com wrote:

> Hi all,
> 
> Suppose I have following code:
> 
> require "./Classes/Printer" # My personal printer class
> 
> Now if I want to run my program Ruby needs to find the Printer.rb file  
> in the Classes directory. But if the script was run like this:
> 
> > cd ~  
> > ruby ~/rubydev/projects/texteditor/main.rb
> 
> Then the current path will be ~ and Ruby won't find  
> ../Classes/Printer.rb  
> How do I solve this problem?

require File.join(File.dirname(\_\_FILE\_\_), 'Classes', 'Printer')

Jim

> **···**
>
> --  
> Jim Menard, jimm@io.com, [http://www.io.com/~jimm](http://www.io.com/~jimm)  
> "I am sure that like Java, [C#] will be a 'no pointer' language, where the  
> most common runtime error will be a 'NULL pointer exception'."  
> &nbsp;&nbsp;&nbsp;&nbsp;-- Jerry Kott, in comp.lang.smalltalk

---

<div class="post-metadata">

**Author:** ![Erik\_Veenstra3](https://avatars.discourse-cdn.com/v4/letter/e/f9ae1b/32.png) [@Erik\_Veenstra3](https://rubytalk.org/u/Erik_Veenstra3)\
**Post date:** [10 August 2005 16:36 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/4 "2005-08-10T16:36:09Z")

</div>

> require "./Classes/Printer" # My personal printer class
> 
> Now if I want to run my program Ruby needs to find the  
> Printer.rb file in the Classes directory. But if the script  
> was run like this:
> 
> $ cd ~  
> $ ruby ~/rubydev/projects/texteditor/main.rb
> 
> Then the current path will be ~ and Ruby won't find  
> ./Classes/Printer.rb
> 
> How do I solve this problem?

$: \<\< File.dirname(File.expand\_path(\_\_FILE\_\_))  
require "Classes/Printer"

Skip the "./" part. "." is already added to $:

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![francisrammeloo](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@francisrammeloo](https://rubytalk.org/u/francisrammeloo)\
**Post date:** [10 August 2005 16:11 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/5 "2005-08-10T16:11:10Z")

</div>

I tried to remove the "./" but that doesn't work.

Using the File.basename($0) trick gives following result:  
&nbsp;&nbsp;&nbsp;LoadError: No such file to load -- irb/Classes/BobParser

Perhaps I should mention that I use following emacs command: C-c C-l  
and that I'm pretty new to emacs : )

---

<div class="post-metadata">

**Author:** ![mathew](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mathew](https://rubytalk.org/u/mathew)\
**Post date:** [11 August 2005 03:21 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/6 "2005-08-11T03:21:10Z")

</div>

Jim Menard wrote:

> require File.join(File.dirname(\_\_FILE\_\_), 'Classes', 'Printer')

I'm inclined to prefer doing

$: \<\< File.dirname(\_\_FILE\_\_)

then using the normal require syntax, just for the sake of clarity.

mathew

> **···**
>
> --  
> \<URL:[http://www.pobox.com/~meta/&gt](http://www.pobox.com/~meta/&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WE HAVE TACOS

---

<div class="post-metadata">

**Author:** ![francisrammeloo](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@francisrammeloo](https://rubytalk.org/u/francisrammeloo)\
**Post date:** [11 August 2005 07:26 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/7 "2005-08-11T07:26:11Z")

</div>

Thank you very much.

This works, but not completely...  
Suppose I have this code:

&nbsp;&nbsp;&nbsp;$: \<\< File.dirname(File.expand\_path(\_\_FILE\_\_))  
&nbsp;&nbsp;&nbsp;require "Classes/Window"  
&nbsp;&nbsp;&nbsp;require "../Modules/InstanceCounter"

Now ruby finds "Classes/Window", but it doesn't find  
"../Modules/InstanceCounter".

Is this normal?  
Is there a solution?

Many thanks in advance for all helpful hints.

Best regards,  
Francis

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [10 August 2005 16:11 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/8 "2005-08-10T16:11:10Z")

</div>

francisrammeloo@hotmail.com wrote:

> I tried to remove the "./" but that doesn't work.
> 
> Using the File.basename($0) trick gives following result:  
> &nbsp;&nbsp;&nbsp;LoadError: No such file to load -- irb/Classes/BobParser

Of course this won't work if you try it from irb. Did you try it in your  
script?

> Perhaps I should mention that I use following emacs command: C-c C-l  
> and that I'm pretty new to emacs : )

Regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![francisrammeloo](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@francisrammeloo](https://rubytalk.org/u/francisrammeloo)\
**Post date:** [10 August 2005 18:26 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/9 "2005-08-10T18:26:10Z")

</div>

require File.join(File.dirname(\_\_FILE\_\_), 'Classes', 'Printer') works  
thanks!

I'll try Erik's trick tomorrow. His' would be even nicer.

Best regards,  
Francis

---

<div class="post-metadata">

**Author:** ![francisrammeloo](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@francisrammeloo](https://rubytalk.org/u/francisrammeloo)\
**Post date:** [11 August 2005 07:56 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/10 "2005-08-11T07:56:10Z")

</div>

I found a solution, but I think it's a bit clumpsy:

&nbsp;&nbsp;&nbsp;$: \<\< File.dirname(File.expand\_path(\_\_FILE\_\_))  
&nbsp;&nbsp;&nbsp;$: \<\< File.dirname(File.expand\_path(\_\_FILE\_\_)).sub(/(.\*)\/\w+/,  
'\1') \<-- added  
&nbsp;&nbsp;&nbsp;require "Classes/Window"  
&nbsp;&nbsp;&nbsp;require "Modules/InstanceCounter" \<-- no "../" needed anymore

Best regards,  
Francis

---

<div class="post-metadata">

**Author:** ![Erik\_Veenstra2](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@Erik\_Veenstra2](https://rubytalk.org/u/Erik_Veenstra2)\
**Post date:** [11 August 2005 11:11 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/11 "2005-08-11T11:11:10Z")

</div>

> I found a solution, but I think it's a bit clumpsy:
> 
> $: \<\< File.dirname(File.expand\_path(\_\_FILE\_\_))  
> $: \<\< File.dirname(File.expand\_path(\_\_FILE\_\_)).sub(/(.\*)\/\w+/, '\1')  
> require "Classes/Window"  
> require "Modules/InstanceCounter" \<-- no "../" needed anymore

I couldn't do it clumpsier...🙂

This is what I would do:

f1 = File.expand\_path(\_\_FILE\_\_)  
d1 = File.dirname(f1)  
d2 = File.dirname(d1)  
$: \<\< d1  
$: \<\< d2  
require "Classes/Window"  
require "Modules/InstanceCounter"

If you want to add more directories, upto the root, use this:

entry = File.expand\_path(\_\_FILE\_\_)  
while (not $:.include?(entry = File.dirname(entry)))  
&nbsp;&nbsp;&nbsp;$: \<\< entry  
end  
require "Classes/Window"  
require "Modules/InstanceCounter"

(Just kidding...)

Just don't use "./" or "../" or any other "relative" references  
to a file in a require statement. Trying setting up $:  
correctly instead and feed require the name of the library  
instead of filenames.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![francisrammeloo](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@francisrammeloo](https://rubytalk.org/u/francisrammeloo)\
**Post date:** [11 August 2005 12:06 UTC](https://rubytalk.org/t/require-and-relative-paths/20056/12 "2005-08-11T12:06:10Z")

</div>

Thanks, that looks a lot better than my "solution"..

vriendelijke groeten,  
Francis
