Forum: Ruby How to get the directory of the current script?

80e8c95efd94e1850838d27024ea1ac1?d=identicon&s=25 laredotornado (Guest)
on 2008-02-20 20:55
(Received via mailing list)
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
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2008-02-20 21:05
(Received via mailing list)
Hi,

File.dirname(__FILE__) will give you the current file's path.  For
your second question, there's no way to do that, unless your code
follows strict code conventions (a-la Rails), or the module provides a
way to query that information.

Hope that helps !
François

2008/2/20, laredotornado <laredotornado@zipmail.com>:
9a56d606d07692444b1e60635eb22df8?d=identicon&s=25 Mark Bush (bush)
on 2008-02-21 14:38
> 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...
87f4d1c05b52314615b1559325f75102?d=identicon&s=25 Kimball Johnson (kimballjohnson)
on 2010-01-02 03:33
laredotornado wrote:
> 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?
2a745e2d109928984604d0b573e55855?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2010-01-02 03:58
(Received via mailing list)
On 02.01.2010 03:33, Kimball Johnson wrote:
> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2010-01-02 19:44
Phillip Gawlowski wrote:
> On 02.01.2010 03:33, Kimball Johnson wrote:
>> 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

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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.