From: "Dan0042 (Daniel DeLorme) via ruby-core" Date: 2025-06-02T15:26:58+00:00 Subject: [ruby-core:122381] [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). Dan0042 (Daniel DeLorme) wrote in #note-2: > 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. Ah, looks like I might have to retract that statement. In bash, `cd ""` is a no-op ``` cd /usr cd "" pwd #=> /usr ``` So `Pathname#join`, which is equivalent to `cd`, has the same behavior. Not sure it makes sense, but at least it's consistent. Although I should note that `Dir.chdir("")` raises an error. lovro-bikic (Lovro Biki��) wrote in #note-3: > Furthermore, the example with a whitespace string shows that `Pathname#join` is capable of adding trailing slashes under certain conditions. That's not a trailing slash, that the file/directory " " inside "usr". But it's true that if you do `.join("a/")` the resulting Pathname has a trailing slash, so indeed `Pathname#join` is capable of adding trailing slashes under certain conditions. > I think there should be a clear way for `Pathname#join` to allow appending trailing slashes to pathnames. Perhaps, but personally I don't think that joining with an empty string should be it. ---------------------------------------- Bug #21391: Inconsistent trailing slash behavior of File.join and Pathname#join with empty strings https://bugs.ruby-lang.org/issues/21391#change-113527 * 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/