File.expand_path('../../Gemfile', __FILE__)
- A somewhat ugly Ruby idiom for getting an absolute file path when you know the path to the current file. Another way to write this:
File.expand_path('../Gemfile', File.dirname(__FILE__))
both are ugly, but the first option is shorter. The first option, however, is also very unintuitive until you hang it. Why extra .. ? (but the second option may give an idea of ββwhy this is necessary).
Here's how it works: File.expand_path returns the absolute path of the first argument relative to the second argument (which by default refers to the current working directory). __FILE__ is the path to the file where the code is located. Since the second argument in this case is the path to the file, and File.expand_path takes the directory, we must insert an additional .. in the path to get the path to the right. Here's how it works:
File.expand_path is mainly implemented as follows (in the following code, path will have the value ../../Gemfile and relative_to will have the value /path/to/file.rb ):
def File.expand_path(path, relative_to=Dir.getwd)
(there is a bit more, it extends ~ to the home directory, etc. - there are probably other problems with the code above)
When calling the code above absolute_path , first get the value /path/to/file.rb/../../Gemfile , then the first .. for each round in the loop will be deleted .. along with the path component in front of it. First /file.rb/.. is deleted, then in the next round /to/.. is deleted, and we get /path/Gemfile .
In short, File.expand_path('../../Gemfile', __FILE__) is a trick to get the absolute path to the file when you know the path to the current file. An optional .. in the relative path is to exclude the file name in __FILE__ .
Theo Dec 18 '10 at 20:11 2010-12-18 20:11
source share