From: "Dan0042 (Daniel DeLorme) via ruby-core" Date: 2025-06-01T13:02:13+00:00 Subject: [ruby-core:122360] [Ruby Bug#21391] Inconsistent trailing slash behavior of File.join and Pathname#join with empty strings Issue #21391 has been updated by Dan0042 (Daniel DeLorme). It's not the only inconsistent behavior: ```ruby File.join("/usr","/var") #=> "/usr/var" Pathname.new("/usr").join("/var").to_s #=> "/var" File.join("/usr","../var") #=> "/usr/../var" Pathname.new("/usr").join("../var").to_s #=> "/var" File.join("/usr","/../var") #=> "/usr/../var" Pathname.new("/usr").join("/../var").to_s #=> "/../var" ``` `File.join` simply joins two strings together with a separator, whereas `Pathname#join` is a logical operation on two paths. It's normal for there to be differences. That being said, I feel that `Pathname.new('/usr').join('')` is a nonsensical operation. It seems to result in a no-op, but it might be better to warn or raise an error. ---------------------------------------- Bug #21391: Inconsistent trailing slash behavior of File.join and Pathname#join with empty strings https://bugs.ruby-lang.org/issues/21391#change-113503 * Author: lovro-bikic (Lovro Biki��) * Status: Open * ruby -v: ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin23] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- ```ruby 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](https://docs.python.org/3.12/library/os.path.html#os.path.join) adds a trailing slash: ```python import os os.path.join('/usr', '') # '/usr/' ``` - [Path.join in Rust](https://doc.rust-lang.org/std/path/struct.Path.html#method.join) adds a trailing slash: ```rust use std::path::{Path}; fn main() { println!("{}", Path::new("/usr").join("").display()); // prints "/usr/" } ``` - [path.join in Node](https://nodejs.org/api/path.html#pathjoinpaths) doesn't add a trailing slash: ```js const path = require('path'); path.join('/usr', ''); // '/usr' ``` - [filepath.Join in Go](https://pkg.go.dev/path/filepath#Join) doesn't add a trailing slash: ```go package main import ("fmt"; "path/filepath") func main() { fmt.Println(filepath.Join("/usr", "")) // prints "/usr" } ``` -- 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/