# Calling different class files using require

**URL:** <https://www.ruby-forum.com/t/calling-different-class-files-using-require/129104>\
**Category:** Ruby\
**Created:** [February 14, 2008, 10:52am UTC](https://www.ruby-forum.com/t/calling-different-class-files-using-require/129104 "2008-02-14T10:52:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [February 14, 2008, 10:52am UTC](https://www.ruby-forum.com/t/calling-different-class-files-using-require/129104/1 "2008-02-14T10:52:44Z")

</div>

Hi all,  
I have 2 files which are placed in two different folders. These folders  
are not under any Classpath & i am using a windows system.

## I have the following snippet of program, which i am stuck with, FILE 1:

class Test

puts " This out is from file1 "

## end

## FILE 2:

require “Test”  
puts “This output is from File2”

## 

If File 1 is and File 2 are in two different locations, i get the  
following error :  
FILE2.rb:1:in `require’: no such file to load – Test (LoadError)  
from run.rb:1  
If both the files are in same folder ,it works but if they are located  
in different locations it fails,  
How can make it work, should i provide anything under “require”

Thanks

---

<div class="post-metadata">

**Author:** ![rubist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c67d28/32.png) [@rubist](https://www.ruby-forum.com/u/rubist)\
**Post date:** [February 14, 2008, 11:01am UTC](https://www.ruby-forum.com/t/calling-different-class-files-using-require/129104/2 "2008-02-14T11:01:04Z")

</div>

On Thu, Feb 14, 2008 at 10:52 AM, Idealone I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> puts “This output is from File2”
> 
> --------------------------------------------------------------------------------- If File 1 is and File 2 are in two different locations, i get the following error : FILE2.rb:1:in `require': no such file to load -- Test (LoadError) from run.rb:1 If both the files are in same folder ,it works but if they are located in different locations it fails, How can make it work, should i provide anything under "require"

First of all, you have to require the name of the file, not the name  
of the class inside, so as you are doing require ‘Test’ I will assume  
that File 1 is called Test.rb.

If the location is always the same relative to File2, I mean, if File2  
can always rely that Test.rb is always  
a sibling directory called xxx, you could do:

require ‘…/xxx/File1’

the $: variable is an array that contains a list of directories that  
require looks for  
files. You can push there that folder if you need to require more than  
one  
file:

$:.unshift “…/xxx”

That variable is initialized, among other things, with whatever you  
pass in the -I  
param to the ruby executable. So you could keep your require ‘Test’ and  
just  
do:

ruby -I …/xxx File2.rb

I hope I don’t have any mistake in the above, it was all from the top  
of my head,  
and not tested, so beware of typos :-).

Hope this helps,

Jesus.
