Feature #1961


Added by yhara (Yutaka HARA) about 11 years ago. Updated about 8 years ago.

Target version:


= Proposal

returns the value of File.dirname(FILE)

According to the google code search, about 60% of uses of FILE
are of the form File.dirname(FILE). Ruby 1.9.2 provides
require_relative for this problem; but File.dirname(FILE) is
not always used for requiring ruby scripts, but also for reading
data files. dir helps these cases.

(Note: my proposal does not include dir_dir this time :-)
It should be discussed in another threads)
Related ticket:

= Problem

File.dirname(FILE) is frequently used and too long.

= Analysis

There are 222 uses of FILE listed by the google code search,
classified into these categories:

(A) 30.6% (68) are used with require and File.dirname.
In Ruby 1.9.2, this case is supported by require_relative.

(B) 31.1% (69) are with File.dirname, but not with require.
For example, reading data files of unit tests.

(C) 21.6% (48) are the idiom, $0 == FILE.

B is as many as A (and even more than C), so it is reasonable to
add a shortcut for File.dirname(FILE) in addition to require_relative.

= Solutions

(1) add a toplevel constant DIR
pros: looks like FILE
cons: adding new keyword

(2) add Kernel#DIR
pros: no new keyword
cons: it should have a lower-case name (like 1.9's method),
because it is not a constant but a method.

(3) add Kernel#dir
pros: no new keyword, and it is clearer than (4) that it has
some associations with FILE.

(4) make FILE to the default argument of File.dirname
pros: no new keyword nor new method
cons: it is not clear that 'File.dirname' is expanded to
the path of directory of FILE.

= Conclusion

I think (3) (Kernel#dir) is the best.

yhara (Yutaka HARA)

Related issues

Related to Ruby master - Feature #642: __DIR__Rejected10/14/2008Actions
Has duplicate Ruby master - Feature #3346: __DIR__ revistedClosedyhara (Yutaka HARA)05/26/2010Actions

Also available in: Atom PDF