Project

General

Profile

Feature #15868

Implement `File.absolute_path?`

Added by deivid (David Rodríguez) 7 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:92794]

Description

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


Files

0001-Add-File.absolute_path.patch (3.24 KB) 0001-Add-File.absolute_path.patch deivid (David Rodríguez), 05/23/2019 09:53 AM
0001-Add-File.absolute_path.patch (3.24 KB) 0001-Add-File.absolute_path.patch With fixed test deivid (David Rodríguez), 05/23/2019 02:07 PM

Associated revisions

Revision 2a166cfe
Added by deivid (David Rodríguez) 3 months ago

Add File.absolute_path? (#2198)

In order to check whether a path is absolute or not in a portable way.

[Feature #15868]

Revision 1c4af1a7
Added by deivid (David Rodríguez) 3 months ago

Add tests for File.absolute_path?

[Feature #15868]

History

Updated by Eregon (Benoit Daloze) 7 months ago

Pathname("C:/foo/bar").absolute? should return true on Windows.
I think it does already:
https://github.com/ruby/ruby/blob/fe3ff5afb07e171fd950623c69abfbabbb2762a3/test/pathname/test_pathname.rb#L278-L282

On non-Windows platforms it will return false, which is correct for non-Windows platforms.

About File.absolute_path?, it sounds good to me.

Updated by deivid (David Rodríguez) 7 months ago

Ouch!

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 #pathname docs.

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 nobu (Nobuyoshi Nakada) 7 months ago

+      File.basename("/foo/bar\\baz").should == true

The method name is wrong, and File.absolute_path?("/foo/bar") should be false on Windows as it doesn't have the drive letter.

Updated by shevegen (Robert A. Heiler) 7 months 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
etc....

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

Updated by phluid61 (Matthew Kerwin) 7 months ago

Out of interest, how does it treat relative paths like C:foo\bar?

(Funny observation: according to Microsoft, \foo\bar is an absolute path in Windows, according to their definition of "absolute path")

Updated by deivid (David Rodríguez) 7 months ago

I added a couple more specs to check that in https://github.com/ruby/ruby/pull/2198/commits/d59a5c93dd4b20aa0898a24fab78a68a2cc84925, but I'm not sure where I can check whether they pass on Windows or not.

#9

Updated by deivid (David Rodríguez) 3 months ago

  • Status changed from Open to Closed

Applied in changeset git|2a166cfea22b90e39e3fe9bafab6b806ed4813f6.


Add File.absolute_path? (#2198)

In order to check whether a path is absolute or not in a portable way.

[Feature #15868]

Also available in: Atom PDF