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)
IMO this method should continue to work even if the user changes the current directory; I don't believe the above implementations do. You can use File.expand_path to expand the path relative to a given directory, namely the current working directory at the time the file was loaded. I haven't tried it, but my first pass would be to modify the above code to:
module Kernel
BASE_DIR = Dir.getwd
def __DIR__
(/^(.+)?:\d+/ =~ caller[0]) ? File.expand_path(File.dirname($1), BASE_DIR) : nil
end
end
though this is still not perfect if a library changes the current working directory when it is required (which is a bad thing to do IMO, but nevertheless possible).
I believe that a perfect solution in pure ruby would be too complex to be worthwhile. I would rather see this implemented in C.
-- Paul Brannan
You could define it as a method:
module Kernel def __DIR__ File.dirname(__FILE__) end endDavid 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 Kerneldef __DIR__ (/^(.+)?:\d+/ =~ caller[0]) ? File.dirname($1) : nil endendHey, thanks! That went right into Facets. If you would like I can credit you in the source code, whomever you are?
T. (Trans)
IMO this method should continue to work even if the user changes the current directory; I don't believe the above implementations do. You can use File.expand_path to expand the path relative to a given directory, namely the current working directory at the time the file was loaded. I haven't tried it, but my first pass would be to modify the above code to:
module Kernel BASE_DIR = Dir.getwd def __DIR__ (/^(.+)?:\d+/ =~ caller[0]) ? File.expand_path(File.dirname($1), BASE_DIR) : nil end endthough this is still not perfect if a library changes the current working directory when it is required (which is a bad thing to do IMO, but nevertheless possible).
I believe that a perfect solution in pure ruby would be too complex to be worthwhile. I would rather see this implemented in C.
-- Paul Brannan