From 80944488774c20010193dcfa4a2a7295ec090c96 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 30 Dec 2025 10:39:03 -0700 Subject: [PATCH] Add Join URI Path Parts as a Ruby TIL --- README.md | 3 ++- ruby/join-uri-path-parts.md | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 ruby/join-uri-path-parts.md diff --git a/README.md b/README.md index 2938f42..0655292 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1712 TILs and counting..._ +_1713 TILs and counting..._ See some of the other learning resources I work on: @@ -1412,6 +1412,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Install Latest Version Of Ruby With asdf](ruby/install-latest-version-of-ruby-with-asdf.md) - [Invoking Rake Tasks Multiple Times](ruby/invoking-rake-tasks-multiple-times.md) - [IRB Has Built-In Benchmarking With Ruby 3](ruby/irb-has-built-in-benchmarking-with-ruby-3.md) +- [Join URI Path Parts](ruby/join-uri-path-parts.md) - [Jump Out Of A Nested Context With Throw/Catch](ruby/jump-out-of-a-nested-context-with-throw-catch.md) - [Last Raised Exception In The Call Stack](ruby/last-raised-exception-in-the-call-stack.md) - [Limit Split](ruby/limit-split.md) diff --git a/ruby/join-uri-path-parts.md b/ruby/join-uri-path-parts.md new file mode 100644 index 0000000..9632500 --- /dev/null +++ b/ruby/join-uri-path-parts.md @@ -0,0 +1,43 @@ +# Join URI Path Parts + +The +[`URI.join`](https://ruby-doc.org/stdlib-2.5.1/libdoc/uri/rdoc/URI.html#method-c-join) +method seems like a handy way to combine a base URL with some subpath. However, +there are some subtle gotchas depending on where forward slashes appear in the +two arguments. + +Let's first look at the, in my opinion, desired behavior: + +```ruby +> URI.join("https://example.com/api/v1/", "users") +=> # +``` + +The base URL has a trailing slash and the path that I want to join to it has no +leading slash. The result is a path where `users` is joined to the end of the +base URL. That's what I'm looking for. + +Now, let's see some variations on the above approach that give results that I +wasn't expecting and don't want. + +```ruby +> URI.join("https://example.com/api/v1", "/users") # 1 +=> # +> URI.join("https://example.com/api/v1", "users") # 2 +=> # +> URI.join("https://example.com/api/v1/", "/users") # 3 +=> # +``` + +1. No trailing slash on the base URL. Leading slash on the path to join. The + path portion of the base URL is wiped out and `/users` is joined in. +2. No trailing slash on the base URL. No leading slash on the path to join. The + `users` path replaces the last part of the path in the base URL. +3. Both a trailing slash in the base URL and a leading slash in the path to + join. Same behavior as 1. + +I have two takeaways from this: +- Use with caution. If I'm going to use `URI.join` for this purpose, I need to + be careful to only use the form in the first code block. +- The `URI.join` method is probably meant to be primarily used to join a domain + (e.g. `http://example.com`) that has no path with some path segment.