Connecting Tech Pros Worldwide Forums | Help | Site Map

Importing other files to run ruby programs

Newbie
 
Join Date: Jan 2008
Posts: 17
#1: Feb 14 '08
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,
File1:
---------------------------------------------------------------------------------
<rubycode>

class Test

puts " This output is from file1 "

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

require "File1"
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"
Hope this is clear!!

Thanks in Advance

Expert
 
Join Date: May 2007
Posts: 213
#2: Feb 14 '08

re: Importing other files to run ruby programs


Include the file path in the require statement.
Expand|Select|Wrap|Line Numbers
  1. require '.../classes/file'
As a helpful note, you can use code tags when posting by clicking the # button. This inserts code tags which displays the code neatly and saves you some typing. See code tags for more info.
Newbie
 
Join Date: Jan 2008
Posts: 17
#3: Feb 18 '08

re: Importing other files to run ruby programs


Quote:

Originally Posted by improvcornartist

Include the file path in the require statement.

Expand|Select|Wrap|Line Numbers
  1. require '.../classes/file'
As a helpful note, you can use code tags when posting by clicking the # button. This inserts code tags which displays the code neatly and saves you some typing. See code tags for more info.

Thanks for your suggestions, I figured out that giving the following will work!!
require 'C:\Documents and Settings\Desktop\File1'
But what i am looking for is not hardcoding the path, since if someone else has to run my program he should be able to do it without touching in the code like modifying the path name etc..
I am using eclipse with RDT plugin, is there anyway i can setup these paths!!
Similar to Build Path for JAVA in eclipse, where in can specify the classpath variables and so on.
Please let me know if any other workaround for this.

Cheers
Reply