Project

General

Profile

Actions

Bug #21391

open

Inconsistent trailing slash behavior of File.join and Pathname#join with empty strings

Added by lovro-bikic (Lovro Bikić) 6 days ago. Updated 2 days ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-darwin23]
[ruby-core:122359]

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:

import os
os.path.join('/usr', '')
# '/usr/'
use std::path::{Path};

fn main() {
    println!("{}", Path::new("/usr").join("").display());
    // prints "/usr/"
}
const path = require('path');

path.join('/usr', '');
// '/usr'
package main

import ("fmt"; "path/filepath")

func main() {
  fmt.Println(filepath.Join("/usr", ""))
  // prints "/usr"
}
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0