# URI.join inconsistency

**URL:** <https://www.ruby-forum.com/t/uri-join-inconsistency/115985>\
**Category:** Ruby\
**Created:** [October 1, 2007, 12:03pm UTC](https://www.ruby-forum.com/t/uri-join-inconsistency/115985 "2007-10-01T12:03:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [October 1, 2007, 12:03pm UTC](https://www.ruby-forum.com/t/uri-join-inconsistency/115985/1 "2007-10-01T12:03:38Z")

</div>

I just got stung by rather annoying feature of URI.join:

irb(main):001:0\> URI.join(‘[http://www.example.com/something](http://www.example.com/something)’,  
‘else’).to\_s  
=\> “[http://www.example.com/else](http://www.example.com/else)”

Note that this completely ignores the ‘something’ in the original URI. I  
had  
assumed that it joined in a similar way to File.join:

irb(main):002:0\> File.join(‘/something’, ‘else’).to\_s  
=\> “/something/else”

I notice that adding a slash works as expected, but why is there a  
difference?  
Is there something in the HTTP spec that defines URI joining like this?

irb(main):001:0\> URI.join(‘[http://www.example.com/something/](http://www.example.com/something/)’,  
‘else’).to\_s  
=\> “[http://www.example.com/something/else](http://www.example.com/something/else)”

Maybe this is a case of incomplete documentation not explaining what the  
join  
is actually doing, this doesn’t seem to be ‘least surprising’ approach  
at the  
moment.

---

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [October 1, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/uri-join-inconsistency/115985/2 "2007-10-01T22:56:08Z")

</div>

On Oct 1, 2007, at 03:02 , Gareth A. wrote:

> =\> “/something/else”
> 
> I notice that adding a slash works as expected, but why is there a  
> difference?

URIs and paths are not the same, “[http://example.com/something](http://example.com/something)” and  
“[http://example.com/something/](http://example.com/something/)” are allowed to return different  
content. URI.join works off the directory, “/” in your case, rather  
than assuming that “/something” is a directory, which may be incorrect.

(My annoyance with this method is that the first argument cannot be a  
URI::Generic.)

---

<div class="post-metadata">

**Author:** ![Gareth\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@Gareth\_A](https://www.ruby-forum.com/u/Gareth_A)\
**Post date:** [October 1, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/uri-join-inconsistency/115985/3 "2007-10-01T23:11:37Z")

</div>

Eric H. wrote:

> than assuming that “/something” is a directory, which may be incorrect.  
> Then again, /something in the filesystem may just as well be a file,  
> rather than a directory, thus if we consider combining a directory name  
> with a file name,

File.join(’/something’, ‘else’)

should logically result /else, while

File.join(’/something/’, ‘else’)

return /something/else. So I guess the behaviour is still inconsistent,  
unless there’s an explicitly different rule for dealing with URLs  
compared to FS paths…

mortee
