Bug #14891

Pathname#join has different behaviour to File.join

Added by robotdana (Dana Sherson) 16 days ago. Updated 6 days ago.

Target version:
ruby -v:
2.6.0-preview2, and before

# => "/b"
File.join('/a'), '/b').to_s
# => "/a/b"

in my case '/b' was in a variable and it wasn't immediately obvious why it wasn't working when I moved to use Pathname

This seems to not be desired behaviour as it's different to File.join, and this case isn't document anywhere.

Can we either change the behaviour to treat the "other" of Pathname#+ as always relative (possibly just removing a leading slash), or add this case to the documentation?


#1 Updated by robotdana (Dana Sherson) 16 days ago

  • ruby -v set to 2.6.0-preview2, and before

#2 [ruby-core:87762] Updated by shevegen (Robert A. Heiler) 16 days ago

This behaviour surprised me too. Is it expected that the information from "/a"
is lost? I have no idea but the documentation does not mention this; perhaps
the above could be added as an example OR the behaviour changed (or both).

I have little to none experience with pathname these days as I seem to handle
paths ... without pathname. :D

Documentation to Pathname .join is at:

#3 [ruby-core:87763] Updated by Hanmac (Hans Mackowiak) 16 days ago

More examples:'/a').join('c', 'b').to_s #=> "/a/c/b"'/a').join('/c', 'b').to_s #=> "/c/b"'/a').join('/c', '/b').to_s #=> "/b"

Why it does this?
because "/c" means start of an absolute path there

#5 [ruby-core:87935] Updated by funny_falcon (Yura Sokolov) 6 days ago

I'd rather say that File.join is currently broken, and it should behave like Pathname.join. But probably I'm missing something.

Also available in: Atom PDF