Calling different class files using require

Discussion in 'Ruby' started by Idealone Ideally, Feb 14, 2008.

  1. 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-forum.com/.
     
    Idealone Ideally, Feb 14, 2008
    #1
    1. Advertising

  2. On Thu, Feb 14, 2008 at 10:52 AM, Idealone Ideally <> 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.
     
    Jesús Gabriel y Galán, Feb 14, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,800
    Thomas Weidenfeller
    Oct 12, 2005
  2. Chris Hare
    Replies:
    3
    Views:
    280
    Chris Hare
    Aug 3, 2010
  3. Jack Wright
    Replies:
    3
    Views:
    249
    Emil Kvarnhammar
    Jul 30, 2004
  4. Bryan
    Replies:
    1
    Views:
    99
    Randy Webb
    Feb 19, 2004
  5. Regent
    Replies:
    9
    Views:
    145
    Jason Hood
    Dec 30, 2003
Loading...

Share This Page