How does one get an absolute absolute file path?

Discussion in 'Ruby' started by James Byrne, Sep 14, 2010.

  1. James Byrne

    James Byrne Guest

    I have a situation where I call a ruby script from a directory that is a
    logical link to the actual one. So for instance the actual path is:

    /home/me/projects/bin/script.rb

    But the path used to call the script is

    /home/me/projects/tmp/testing/bin/script.rb

    where tmp/testing/bin is a logical link to projects/bin.

    With Ruby-1.8.7 using a simple require in the script, like this, works
    fine:

    require File.dirname(__FILE__) + "../lib/library"

    However, this no longer works in 1.9.2 because of the decision, asinine
    in my opinion, to remove . from the default load path. So, what I am
    trying to discover is how best to provide an absolute path which gives
    the same result. The problem is that when I build such a path using
    File.expand_path I end up with this:

    /home/me/projects/tmp/testing/lib/library

    which fails because there is no /home/me/projects/tmp/testing/lib
    directory.

    My question is: Is there any way to obtain the absolute path for
    __FILE__ such that it returns the path to its actual location and not
    one that includes any logical links?
    --
    Posted via http://www.ruby-forum.com/.
    James Byrne, Sep 14, 2010
    #1
    1. Advertising

  2. James Byrne

    Robert Dober Guest

    On Tue, Sep 14, 2010 at 5:05 PM, James Byrne <> wrote:
    > I have a situation where I call a ruby script from a directory that is a
    > logical link to the actual one. =A0So for instance the actual path is:
    >
    > /home/me/projects/bin/script.rb
    >
    > But the path used to call the script is
    >
    > /home/me/projects/tmp/testing/bin/script.rb
    >
    > where tmp/testing/bin is a logical link to projects/bin.
    >
    > With Ruby-1.8.7 using a simple require in the script, like this, works
    > fine:
    >
    > require File.dirname(__FILE__) + "../lib/library"
    >
    > However, this no longer works in 1.9.2 because of the decision, asinine
    > in my opinion, to remove . from the default load path. =A0So, what I am
    > trying to discover is how best to provide an absolute path which gives
    > the same result. =A0The problem is that when I build such a path using
    > File.expand_path I end up with this:
    >
    > /home/me/projects/tmp/testing/lib/library
    >
    > which fails because there is no /home/me/projects/tmp/testing/lib
    > directory.
    >
    > My question is: Is there any way to obtain the absolute path for
    > __FILE__ such that it returns the path to its actual location and not
    > one that includes any logical links?


    File.expand_path( File.dirname( __FILE__ ))
    normally does the trick for me.

    HTH
    R.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --=20
    The best way to predict the future is to invent it.
    -- Alan Kay
    Robert Dober, Sep 14, 2010
    #2
    1. Advertising

  3. James Byrne

    James Byrne Guest

    Robert Dober wrote:
    > On Tue, Sep 14, 2010 at 5:05 PM, James Byrne <>
    > wrote:
    >>
    >>
    >> /home/me/projects/tmp/testing/lib/library
    >>
    >> which fails because there is no /home/me/projects/tmp/testing/lib
    >> directory.
    >>
    >> My question is: Is there any way to obtain the absolute path for
    >> __FILE__ such that it returns the path to its actual location and not
    >> one that includes any logical links?

    >
    > File.expand_path( File.dirname( __FILE__ ))
    > normally does the trick for me.
    >
    > HTH
    > R.


    This construction returns the logical link as part of the path. I want
    the actual file system path to the script ignoring the logical link.
    --
    Posted via http://www.ruby-forum.com/.
    James Byrne, Sep 14, 2010
    #3
  4. James Byrne

    James Byrne Guest

    Glenn Jackman wrote:
    > At 2010-09-14 11:35AM, "James Byrne" wrote:
    >> >> My question is: Is there any way to obtain the absolute path for

    >> the actual file system path to the script ignoring the logical link.

    > By "logical link" I assume you mean symbolic link -- I've never heard of
    > a logical link.


    You are correct, I was writing of symbolic links.

    >
    > You want the pathname package:
    >
    > require 'pathname'
    > path = Pathname.new(__FILE__)
    > p path.realpath


    Thank you. This seems to provide the answer that I sought.
    --
    Posted via http://www.ruby-forum.com/.
    James Byrne, Sep 14, 2010
    #4
    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. Matt
    Replies:
    3
    Views:
    19,539
    Sudsy
    Jul 8, 2004
  2. Replies:
    10
    Views:
    3,535
  3. Thomas Guettler
    Replies:
    3
    Views:
    754
    Andrei
    Oct 27, 2003
  4. Martin Arvidsson, Visual Systems AB

    Q: Get absolute path to a file.

    Martin Arvidsson, Visual Systems AB, Apr 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    596
    George Ter-Saakov
    Apr 7, 2008
  5. Nigel Wilkinson
    Replies:
    2
    Views:
    382
    Nigel Wilkinson
    Jul 25, 2005
Loading...

Share This Page