I found this post that covers the discussion of introducing a new Ruby Kernel method __dir__, which would return the current directory containing the source code file, __FILE__.


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


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

* code: http://gist.github.com/170336
* result: http://route477.net/files/__file__.html


(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.

Reading further in the discussion, you can read the debate of __DIR__ versus __dir__. Matz signed off on __dir__.

So now you know. The command, __dir__, is a Ruby Kernel method and not a constant, like __FILE__.

Happy Hacking.

– Chris