Actions
Bug #21391
openInconsistent trailing slash behavior of File.join and Pathname#join with empty strings
Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin23]
Description
File.join('/usr', '')
# => "/usr/"
Pathname.new('/usr').join('').to_s
# => "/usr" # no trailing slash
File.join('/usr', ' ')
# => "/usr/ "
Pathname.new('/usr').join(' ').to_s
# => "/usr/ "
File.join
with an empty string adds a trailing slash, Pathname#join
doesn't.
When Pathname#join
argument is a string with empty whitespace, a trailing slash is added (plus whitespace).
I think it's a common use-case to append a trailing slash to Pathname
, and currently you have to resort to other methods such as string interpolation (e.g. in Rails, "#{Rails.root}/"
) or File.join
(e.g. File.join(Rails.root, '')
).
In other popular languages, both approaches have been taken:
-
os.path.join
in Python adds a trailing slash:
import os
os.path.join('/usr', '')
# '/usr/'
- Path.join in Rust adds a trailing slash:
use std::path::{Path};
fn main() {
println!("{}", Path::new("/usr").join("").display());
// prints "/usr/"
}
- path.join in Node doesn't add a trailing slash:
const path = require('path');
path.join('/usr', '');
// '/usr'
- filepath.Join in Go doesn't add a trailing slash:
package main
import ("fmt"; "path/filepath")
func main() {
fmt.Println(filepath.Join("/usr", ""))
// prints "/usr"
}
Actions
Like0
Like0Like0Like0Like0Like0