Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > best way to access directory relational to current file

Reply
Thread Tools

best way to access directory relational to current file

 
 
phil.swenson@gmail.com
Guest
Posts: n/a
 
      01-28-2007
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

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      01-28-2007
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

 
Reply With Quote
 
 
 
 
Daniel DeLorme
Guest
Posts: n/a
 
      01-28-2007
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

 
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
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Portable way of obtaining current working directory...... Shaun Heveron C++ 6 10-28-2004 12:06 AM
Objects/Relational Mapping Software - Which one is the best? NOSPAM ASP .Net 0 10-19-2004 01:37 PM
Objects/Relational Mapping Software - Which one is the best? NOSPAM ASP .Net 0 10-19-2004 01:36 PM
Perlish way to get absolute path of current working directory? Bob Walton Perl Misc 9 09-26-2003 01:48 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