best way to access directory relational to current file

P

phil.swenson

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
 
D

Daniel Berger

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
 
D

Daniel DeLorme

Daniel said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top