[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling different class files using require

Idealone Ideally

2/14/2008 9:53:00 AM

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
--
Posted via http://www.ruby-....

1 Answer

Jesús Gabriel y Galán

2/14/2008 10:00:00 AM

0

On Thu, Feb 14, 2008 at 10:52 AM, Idealone Ideally <shekarls@gmail.com> wrote:
> 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.