CARVIEW |
Select Language
HTTP/2 200
x-amz-id-2: U0ND/LyRv2kgrfFUMxR44+IPjl0ukUgtaksPAIL6+Ls0gXG1O1AnFE6u7HzMYNkhq24ECtQaKus=
x-amz-request-id: C9Y1DQ5T1SNHT7ZK
last-modified: Sun, 01 Jun 2025 09:51:21 GMT
etag: "14660aacfc9fb7507aa72220c6f17a3e"
x-amz-server-side-encryption: AES256
server: AmazonS3
content-encoding: gzip
via: 1.1 varnish, 1.1 varnish
content-type: text/plain; charset=utf-8
accept-ranges: bytes
age: 0
date: Sat, 26 Jul 2025 00:13:18 GMT
x-served-by: cache-tyo11956-TYO, cache-bom-vanm7210037-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753488798.785711,VS0,VE290
vary: Accept-Encoding
content-length: 1113
From: lovro-bikic via ruby-core
Date: 2025-06-01T09:42:10+00:00
Subject: [ruby-core:122359] [Ruby Bug#21391] Inconsistent trailing slash behavior of File#join and Pathname#join with empty strings
Issue #21391 has been reported by lovro-bikic (Lovro Biki��).
----------------------------------------
Bug #21391: Inconsistent trailing slash behavior of File#join and Pathname#join with empty strings
https://bugs.ruby-lang.org/issues/21391
* 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/