Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How does one get an absolute absolute file path?

Reply
Thread Tools

How does one get an absolute absolute file path?

 
 
James Byrne
Guest
Posts: n/a
 
      09-14-2010
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/.

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      09-14-2010
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

 
Reply With Quote
 
 
 
 
James Byrne
Guest
Posts: n/a
 
      09-14-2010
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/.

 
Reply With Quote
 
James Byrne
Guest
Posts: n/a
 
      09-14-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Get ubuntu ! Get ubuntu ! Get ubuntu ! Get ubuntu ! Getubuntu Windows 64bit 1 06-01-2009 08:54 AM
Q: Get absolute path to a file. Martin Arvidsson, Visual Systems AB ASP .Net 1 04-07-2008 03:47 PM
how does one get an existing .class file in a byteBuffer? enzo.660@gmail.com Java 2 04-12-2007 05:40 PM
Get Absolute Url of FileSystem file nfalconer ASP .Net 0 07-22-2003 07:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57