File.join compared with Pathname#join

This difference was surprising to me:

irb (2.4.4)
require ‘pathname’
Pathname.new(‘foo’).join(‘/bar’)
=> #<Pathname:/bar>
Pathname.new(‘foo’).join(‘bar’)
=> #<Pathname:foo/bar>

File.join(‘foo’, ‘/bar’)
=> “foo/bar”
File.join(‘foo’, ‘bar’)
=> “foo/bar”

Is there a reasonable explanation that I can’t think of for this behavior?

Walter

This difference was surprising to me:

​that is fine.

irb (2.4.4)
require ‘pathname’
Pathname.new(‘foo’).join(‘/bar’)
=> #<Pathname:/bar>

​when you join an absolute path, it overrides (unjoins) all other existing
paths​ and makes it the new base path.

Pathname.new("foo").join("/bar").join("baz")

=> #<Pathname:/bar/baz>

​> Pathname.new("foo").join("/bar").join("/ruby")
=> #<Pathname:/ruby>

Pathname.new(‘foo’).join(‘bar’)

=> #<Pathname:foo/bar>

​ok

File.join(‘foo’, ‘/bar’)
=> “foo/bar”
File.join(‘foo’, ‘bar’)
=> “foo/bar”

​File is very basic; just treats them as string w the file separator (yet
discards separator if you prepend one : ) Since file returns string
chaining fails thereafter,

File.join("bar","foo").join("ruby")

NoMethodError: undefined method `join' for "bar/foo":String

Is there a reasonable explanation that I can’t think of for this behavior?

​i think ruby doc is not so clear on this behaviour (havent check current
yet), but
this is common in other languages (eg python) or on other platforms​

···

On Fri, Jul 20, 2018 at 2:50 AM, Walter Lee Davis <waltd@wdstudio.com> wrote:

Walter

--

many thanks,
--botp