Jon-Michael Deldin

BMX, bike trials, & software from the Pacific Northwest

Ruby URI.join Gotcha

Quick tip: Don’t be dumb and assume Ruby’s URI.join is like Array.join. I made a seemingly innocent change to add some new functionality to an old API:

URI.join("", "/admin/important_base", "/some_endpoint_argument")

I expected this:

but I got this instead:


File.join gives the closest functionality, as far as handling as slashes intelligently:

File.join("", "/admin/important_base/", "/some_endpoint_argument")
#=> ""
#=> #<URI::HTTP>

The other obvious candidates – Array.join or string concatenation – require managing forward-slashes, which is what I was hoping to avoid in the first place.

* * *