From: "ufuk (Ufuk Kayserilioglu) via ruby-core" Date: 2024-08-14T12:59:36+00:00 Subject: [ruby-core:118852] [Ruby master Feature#20676] Pathnames aren't Comparable 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") => # irb(main):002> p2 = Pathname.new("/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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/