Calling different class files using require

I

Idealone Ideally

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:
---------------------------------------------------------------------------------
<rubycode>

class Test

puts " This out is from file1 "

end
<rubycode>
---------------------------------------------------------------------------------
FILE 2:
---------------------------------------------------------------------------------
<rubycode>

require "Test"
puts "This output is from File2"

<rubycode>
---------------------------------------------------------------------------------
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
 
J

Jesús Gabriel y Galán

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:
---------------------------------------------------------------------------------
<rubycode>

class Test

puts " This out is from file1 "

end
<rubycode>
---------------------------------------------------------------------------------
FILE 2:
---------------------------------------------------------------------------------
<rubycode>

require "Test"
puts "This output is from File2"

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top