Misc #19155
closeddocumentation of Pathname#join with absolute path
Description
Pathname#join ignores previous directory name before absolute path
Is this intentional?
irb(main):002:0> require 'pathname'
=> true
irb(main):003:0> Pathname('/foo').join('bar', 'baz')
=> #<Pathname:/foo/bar/baz>
irb(main):004:0> Pathname('/foo').join('bar', '/baz')
=> #<Pathname:/baz>
irb(main):005:0>
➜✗ ruby -v
ruby 3.0.4p208 (2022-04-12 revision 3fa771dded) [x86_64-darwin19]
I found that this behavior is intentional from test code but I cannot found reason.
https://github.com/ruby/pathname/blob/master/test/pathname/test_pathname.rb#L248-L261
There was no description of this behavior in the documentation.
Updated by sawa (Tsuyoshi Sawada) almost 2 years ago
There is a description.
The doc https://docs.ruby-lang.org/ja/latest/class/Pathname.html#I_JOIN says:
path0 = Pathname("/usr") # Pathname:/usr
path0 = path0.join("bin/ruby") # Pathname:/usr/bin/ruby
# 上記の path0 の処理は下記の path1 と同様のパスになります
path1 = Pathname("/usr") + "bin/ruby" # Pathname:/usr/bin/ruby
path0 == path1 #=> true
where "上記の path0
の処理は下記の path1
と同様のパスになります" translates to English as "the path of path0
above is handled on a par with path1
below", which means that join
follows the specification of +
. And here https://docs.ruby-lang.org/ja/latest/class/Pathname.html#I_--2B it says:
self + other -> Pathname[permalink][rdoc][edit]
self / other -> Pathname
パス名を連結します。つまり、other を self からの相対パスとした新しい Pathname オブジェクトを生成して返します。
other が絶対パスなら単に other と同じ内容の Pathname オブジェクトが返されます。
where "other
が絶対パスなら単に other
と同じ内容の Pathname
オブジェクトが返されます。" translates to English as "if other
is an absolute path, then simply a Pathname
object with the same content as other
would be returned".
Updated by nobu (Nobuyoshi Nakada) almost 2 years ago
- Status changed from Open to Closed
Applied in changeset git|b8a73e704ddc77db36317dda293e99fb0ee641f4.
[ruby/pathname] [Misc #19155] [DOC] Addion of absolute paths