How to get the directory of the current script?

L

laredotornado

Hi,

How do I get the directory path of the file that is currently
executing?

Also, if I have a module, within a function, how do I get the parent
directory (full path) of that module?

Thanks, - Dave
 
F

François Beausoleil

SGksCgpGaWxlLmRpcm5hbWUoX19GSUxFX18pIHdpbGwgZ2l2ZSB5b3UgdGhlIGN1cnJlbnQgZmls
ZSdzIHBhdGguICBGb3IKeW91ciBzZWNvbmQgcXVlc3Rpb24sIHRoZXJlJ3Mgbm8gd2F5IHRvIGRv
IHRoYXQsIHVubGVzcyB5b3VyIGNvZGUKZm9sbG93cyBzdHJpY3QgY29kZSBjb252ZW50aW9ucyAo
YS1sYSBSYWlscyksIG9yIHRoZSBtb2R1bGUgcHJvdmlkZXMgYQp3YXkgdG8gcXVlcnkgdGhhdCBp
bmZvcm1hdGlvbi4KCkhvcGUgdGhhdCBoZWxwcyAhCkZyYW7Dp29pcwoKMjAwOC8yLzIwLCBsYXJl
ZG90b3JuYWRvIDxsYXJlZG90b3JuYWRvQHppcG1haWwuY29tPjoKPiBIaSwKPgo+ICBIb3cgZG8g
SSBnZXQgdGhlIGRpcmVjdG9yeSBwYXRoIG9mIHRoZSBmaWxlIHRoYXQgaXMgY3VycmVudGx5Cj4g
IGV4ZWN1dGluZz8KPgo+ICBBbHNvLCBpZiBJIGhhdmUgYSBtb2R1bGUsIHdpdGhpbiBhIGZ1bmN0
aW9uLCBob3cgZG8gSSBnZXQgdGhlIHBhcmVudAo+ICBkaXJlY3RvcnkgKGZ1bGwgcGF0aCkgb2Yg
dGhhdCBtb2R1bGU/Cj4KPiAgVGhhbmtzLCAtIERhdmUKPgo+CgoKLS0gCkZyYW7Dp29pcyBCZWF1
c29sZWlsCmh0dHA6Ly9ibG9nLnRla3NvbC5pbmZvLwpodHRwOi8vcGlzdG9uLnJ1Ynlmb3JnZS5v
cmcvCg==
 
M

Mark Bush

File.dirname(__FILE__) will give you the current file's path.

The current file may be accessed with a relative path. If you need the
absolute path,
make sure you expand it: File.expand_path(File.dirname(__FILE__))
Also, if I have a module, within a function, how do I get the parent
directory (full path) of that module?

If __FILE__ is used in that module, it will refer to the file containing
that module.
If the module is under your control, you can add a method that provides
this:

module MyModule
def this_file
__FILE__
end
end

Otherwise, as has been noted, since a module name does not have to have
any
relation to the filename it is in, there is no way (AFAIK) to find the
file containing a
particular module unless you open the current file and parse for any
require
or load calls (recursively using the current environment for searching
for files)
and look for the module declaration line. (Note that a file may have
been required
using a path, so you must parse the current script to find where files
have been
located.)

Of course, a module could have content added in multiple files...
 
K

Kimball Johnson

laredotornado said:
Hi,

How do I get the directory path of the file that is currently
executing?

Also, if I have a module, within a function, how do I get the parent
directory (full path) of that module?

Thanks, - Dave


As to getting the parent directory of a file, try this:

File.dirname(File.dirname(__FILE__))

or for the absoluter path:

File.expand_path(File.dirname(File.dirname(__FILE__)))

dumb but smart?
 
P

Phillip Gawlowski

As to getting the parent directory of a file, try this:

File.dirname(File.dirname(__FILE__))

or for the absoluter path:

File.expand_path(File.dirname(File.dirname(__FILE__)))

dumb but smart?

require "futils"
puts Dir.pwd
 
B

Brian Candler

Phillip said:
require "futils"
puts Dir.pwd

Note 1: the process's current working directory is in general unrelated
to the directory where the script itself is located.

Note 2: you don't need to load any library to get access to Dir.pwd
 

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,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top