Currently there's no way to check whether a path is absolute or not in a way that works accross OSs. The pathname library has the #absolute? method, but that only checks whether the path starts with a slash, which is not appropriate for Windows.
I thought of reimplementing it as something like File.absolute_path(self) == self, but that would mean accessing the filesystem, which I don't think we want here.
I also thought of implementing the "windows letter checks" in the pathname's library, but then I saw that those are already implemented in file.c, so I thought it would be a good idea to expose those. So I propose to add File.absolute_path? for this.
If this is accepted, I can do a follow-up PR to change Pathname#absolute? to delegate to File.absolute_path?.
What do you think?
I attach a patch to add
File.absolute_path? here (I also opened a PR on Github: https://github.com/ruby/ruby/pull/2198).
Updated by Eregon (Benoit Daloze) almost 5 years ago
Pathname("C:/foo/bar").absolute? should return
true on Windows.
I think it does already:
On non-Windows platforms it will return
false, which is correct for non-Windows platforms.
File.absolute_path?, it sounds good to me.
Updated by deivid (David Rodríguez) almost 5 years ago
I actually read the
Pathname docs, which state:
"However non-Unix pathnames are supported experimentally", in the main section.
"It returns true if the pathname begins with a slash", in the
And assumed this didn't work on Windows.
If this is the case, I'm not too strong towards adding
File.absolute_path? although it still seems handy and simple?
Updated by shevegen (Robert A. Heiler) almost 5 years ago
I have no particular pro/con opinion on the suggestion itself, but I should state that since Pathname was mentioned - I
myself use File.* related methods exclusively. I used to use pathname in the past but I sort of gave up on it eventually,
primarily because File.* seems to work just fine (and FileUtils); partially also because of having to do require 'pathname'
and my general impression that pathname is more clumsy to work with than File (but this is a subjective opinion). So this
comment from me here is mostly about pathname to File.* related methods - to me, in the current use cases, I could happily
use File.* but it would be unlikely for me to go back to when I used to use pathname.
So when File.* and Pathname is compared, I would like to point out that the way how ruby users use either of them may
be different rather than equal/synonymous. (I am not sure if this comment is very useful but I wanted to point this out
at the least once.)
Having said that, I personally have not had a need for File.absolute_path?() yet. I use File.absolute_path() a lot,
though, often because I have to deal/handle symlinks through ruby, including re-symlinking, removing old symlinks
I can say that this may be useful, but personally I honestly have not yet had a need for it.
As for the documentation - I guess it could easily be made more accurate or slightly lengthier in this case; I assume
that this may depend a lot on the operating system, since windows is mentioned here. Perhaps the documentation can
add a sentence about present windows support - I have worked only very little with windows, but the ruby code I write
tends to work very well on windows out of the box, without even me trying to do much at all (even ruby-gtk stuff
works on windows, which is pretty cool).