best way to access directory relational to current file

Discussion in 'Ruby' started by phil.swenson@gmail.com, Jan 28, 2007.

  1. Guest

    I'm trying to find the full path to a file. File.dirname(__FILE__)
    always returns ".", which doesn't really do me any good. The working
    directory is not always the same as where the file is, so I can't use
    the "." in any useful way.

    What I'm trying to do is locate where a particular file is because I
    know where other files are in relation to this particular file. I
    don't know what the working directory will be though, anyone could
    execute the ruby file originating from any location.

    Thanks for any thoughts....

    phil
    , Jan 28, 2007
    #1
    1. Advertising

  2. wrote:
    > I'm trying to find the full path to a file. File.dirname(__FILE__)
    > always returns ".", which doesn't really do me any good. The working
    > directory is not always the same as where the file is, so I can't use
    > the "." in any useful way.


    <snip>

    I think you want File.expand_path(File.dirname(__FILE__))

    Regards,

    Dan
    Daniel Berger, Jan 28, 2007
    #2
    1. Advertising

  3. Daniel Berger wrote:
    > wrote:
    >> I'm trying to find the full path to a file. File.dirname(__FILE__)
    >> always returns ".", which doesn't really do me any good. The working
    >> directory is not always the same as where the file is, so I can't use
    >> the "." in any useful way.

    >
    > I think you want File.expand_path(File.dirname(__FILE__))


    I tend to use this idiom a lot:
    require File.expand_path(__FILE__+'/../relative/path/to/file')

    For ruby, "file" and "dummydir/../file" are considered as 2 different
    files, so I think it's a good idea to always use absolute directories
    when doing a require.

    Daniel
    Daniel DeLorme, Jan 28, 2007
    #3
    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. NOSPAM
    Replies:
    0
    Views:
    305
    NOSPAM
    Oct 19, 2004
  2. NOSPAM
    Replies:
    0
    Views:
    352
    NOSPAM
    Oct 19, 2004
  3. Shaun Heveron
    Replies:
    6
    Views:
    469
    Lieven
    Oct 28, 2004
  4. Alex Martelli
    Replies:
    25
    Views:
    1,434
    MrJean1
    Dec 1, 2005
  5. Bob Walton
    Replies:
    9
    Views:
    170
    David K. Wall
    Sep 26, 2003
Loading...

Share This Page