[ruby-core:118844] [Ruby master Feature#20676] Pathnames aren't Comparable

Issue #20676 has been reported by gmcgibbon (Gannon McGibbon). ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676 * Author: gmcgibbon (Gannon McGibbon) * Status: Open ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by byroot (Jean Boussier).
to check if pathnames are subdirectories of each other
I don't understand. ```ruby
Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s => true Pathname("/a/b").to_s <= Pathname("/a/c").to_s => true
`<=` doesn't tell you the right hand-side is a subdirectory of the left hand-side.
----------------------------------------
Feature #20676: Pathnames aren't Comparable
https://bugs.ruby-lang.org/issues/20676#change-109414
* Author: gmcgibbon (Gannon McGibbon)
* Status: Open
----------------------------------------
👋
I was working with Pathnames recently and noticed that I could do:
```rb
Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s
but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by nobu (Nobuyoshi Nakada). Probably you may want to do: ```ruby (Pathname("/a/b/c").to_s+"/").start_with?(Pathname("/a/b").to_s+"/") ``` ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676#change-109416 * Author: gmcgibbon (Gannon McGibbon) * Status: Open ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by Hanmac (Hans Mackowiak). nobu (Nobuyoshi Nakada) wrote in #note-2:
Probably you may want to do:
```ruby (Pathname("/a/b/c").to_s+"/").start_with?(Pathname("/a/b").to_s+"/") ```
Pathname class already has this kind of logic in the `<=>` function it just doesn't include the `Comparable` module to add the other compare functions like `<=` too ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676#change-109418 * Author: gmcgibbon (Gannon McGibbon) * Status: Feedback ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by ufuk (Ufuk Kayserilioglu). Hanmac (Hans Mackowiak) wrote in #note-4:
nobu (Nobuyoshi Nakada) wrote in #note-2:
Probably you may want to do:
```ruby (Pathname("/a/b/c").to_s+"/").start_with?(Pathname("/a/b").to_s+"/") ```
Pathname class already has this kind of logic in the `<=>` function
It doesn't, at least not in the way the original poster asked for or as @nobu suggested. This is clear from @byroot's [example above](https://bugs.ruby-lang.org/issues/20676#note-1). The `Pathname#<=>` method only does a case-insensitive string comparison of the pathnames, which does not imply any subdirectory relationship between the two. In case another example is helpful: ```ruby irb(main):001> p1 = Pathname.new("/a/b/c") => #<Pathname:/a/b/c> irb(main):002> p2 = Pathname.new("/a/b/d") => #<Pathname:/a/b/d> irb(main):003> p1 <=> p2 => -1 ``` Lexicographically, it is true that `"/a/b/c"` would sort before `"/a/b/d"`, but that does NOT imply any subdirectory relationship as you can see. ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676#change-109419 * Author: gmcgibbon (Gannon McGibbon) * Status: Feedback ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by Eregon (Benoit Daloze). I think we should add `Pathname#start_with?(path)` for convenience, or maybe `Pathname#inside?(path)`. FWIW it already exists in my `path` gem: https://github.com/eregon/path/blob/a5e1d4d35a66466cfe8ab201f67982916cbbe845... ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676#change-109420 * Author: gmcgibbon (Gannon McGibbon) * Status: Feedback ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by byroot (Jean Boussier).
I think we should add Pathname#start_with?(path) for convenience, or maybe Pathname#inside?(path).
Yes, having methods to check if a path is a child or parent of the other would be very convenient. Should be opened as a new feature request though. ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676#change-109421 * Author: gmcgibbon (Gannon McGibbon) * Status: Feedback ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/

Issue #20676 has been updated by gmcgibbon (Gannon McGibbon). It looks like I got it wrong. `<=>` does case-sensitive matching and doesn't behave the way I thought it did. Generally, I agree `Pathname#inside?(path)` would be a good addition that addresses my concern. Thank you! ---------------------------------------- Feature #20676: Pathnames aren't Comparable https://bugs.ruby-lang.org/issues/20676#change-109422 * Author: gmcgibbon (Gannon McGibbon) * Status: Feedback ---------------------------------------- 👋 I was working with Pathnames recently and noticed that I could do: ```rb Pathname("/a/b").to_s <= Pathname("/a/b/c").to_s ``` but could not do: ```rb Pathname("/a/b") <= Pathname("/a/b/c") ``` to check if pathnames are subdirectories of each other. Pathname implements [`<=>`](https://docs.ruby-lang.org/en/master/Pathname.html#method-i-3C-3D-3E) with case insensitive matching for use-cases like this, but does not include Comparable. I think Pathname should include Comparable. I've opened a [PR here](https://github.com/ruby/pathname/pull/40) for your consideration. Thanks! -- https://bugs.ruby-lang.org/
participants (6)
-
byroot (Jean Boussier)
-
Eregon (Benoit Daloze)
-
gmcgibbon (Gannon McGibbon)
-
Hanmac (Hans Mackowiak)
-
nobu (Nobuyoshi Nakada)
-
ufuk (Ufuk Kayserilioglu)