ruby picture

RCR 309: __DIR__

Submitted by transami (Sat Jul 30 21:54:29 UTC 2005)

Abstract

Very simple. We have __FILE__, I sometimes need File.dirname(__FILE__). It would be nice if __DIR__ were equal to that. I have tried to use __DIR__ = File.dirname(__FILE__). But Ruby won't recognize it as a constant. Even so it would be nice if it were built in.

Problem

At the very least:

  irb(main):001:0> __DIR__ = File.dirname(__FILE__)
  => "."
  irb(main):002:0> def x
  irb(main):003:1>   p __DIR__
  irb(main):004:1> end
  => nil
  irb(main):005:0> x
  NameError: undefined local variable or method `__DIR__' for main:Object
        from (irb):3:in `x'
        from (irb):5

But again built-in would be even better.

Proposal

Add __DIR__ as a built in constant.

Analysis

__DIR__ is useful info and the enduser can't get a similar constant to __FILE__ (w/o a hack at least).

Implementation

__DIR__ = File.dirname(__FILE__)
ruby picture
Comments Current voting
Hi --

You could define it as a method:

  module Kernel
    def __DIR__
      File.dirname(__FILE__)
    end
  end 

David Black


Ah, Kernel method. Of course. Thanks. Had constant stuck in my mind for some reason.

T.


I should point out for those who don;t see it right off, that this is still a viable addition despite it being definable as a kernel method. Notice that the kernel method will always return the directory location of the file it is defined in, not the one you call it from. So it doesn't really work.


module Kernel
  def __DIR__
    (/^(.+)?:\d+/ =~ caller[0]) ? File.dirname($1) : nil
  end
end


Hey, thanks! That went right into Facets. If you would like I can credit you in the source code, whomever you are?

T. (Trans)


Strongly opposed 1
Opposed 1
Neutral 0
In favor 1
Strongly advocate 2
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .