# URI.join broken?

**URL:** <https://www.ruby-forum.com/t/uri-join-broken/57327>\
**Category:** Ruby\
**Created:** [April 9, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/uri-join-broken/57327 "2006-04-09T21:09:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 9, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/1 "2006-04-09T21:09:09Z")

</div>

Hi,

I have been looking at the documentation and the source of URI.join and  
can’t see what I have misunderstood.

[http://ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html#M009245](http://ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html#M009245)

I except URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1)”, “test2”) in an URI which looks  
like [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) and not like [test.de Archiv | Stiftung Warentest](http://test.de/test2). And  
from reading the documentation I even expect that  
URI.join(“[http://test.de/](http://test.de/)”, “test1”, “test2”) results in  
[test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) and not [test.de Archiv | Stiftung Warentest](http://test.de/test2)

So, that do I misunderstand about the URI class? 🙂 I am using Ruby  
1.8.4 from Darwinports on Mac OS X.

Best regards,  
Oliver

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 9, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/2 "2006-04-09T22:48:05Z")

</div>

On 09/04/06, Oliver A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I except URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1)”, “test2”) in an URI which looks  
> like [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) and not like [test.de Archiv | Stiftung Warentest](http://test.de/test2). And  
> from reading the documentation I even expect that  
> URI.join(“[http://test.de/](http://test.de/)”, “test1”, “test2”) results in  
> [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) and not [test.de Archiv | Stiftung Warentest](http://test.de/test2)

That’s a strange result. It works as expected for me, also using 1.8.4  
via Darwin Ports on OS X:

URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1)”, “test2”).to\_s #=\>  
“[test.de Archiv | Stiftung Warentest](http://test.de/test2)”

I don’t think you’re missing anything: you are getting anomalous  
output. Are you testing it with the same input data?

Paul.

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 10, 2006, 12:22am UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/3 "2006-04-10T00:22:16Z")

</div>

Paul B. wrote:

> On 09/04/06, Oliver A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I except URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1)”, “test2”) in an URI which looks  
> > like [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) and not like [test.de Archiv | Stiftung Warentest](http://test.de/test2). And  
> > from reading the documentation I even expect that  
> > URI.join(“[http://test.de/](http://test.de/)”, “test1”, “test2”) results in  
> > [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) and not [test.de Archiv | Stiftung Warentest](http://test.de/test2)
> 
> That’s a strange result. It works as expected for me, also using 1.8.4  
> via Darwin Ports on OS X:
> 
> URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1)”, “test2”).to\_s #=\>  
> “[test.de Archiv | Stiftung Warentest](http://test.de/test2)”

Can you explain to me why this is correct? I would expect  
[test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) as the result of your call.

Oliver

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 10, 2006, 12:56am UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/4 "2006-04-10T00:56:43Z")

</div>

On Apr 9, 2006, at 6:22 PM, Oliver A. wrote:

> Can you explain to me why this is correct? I would expect  
> [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) as the result of your call.
> 
> Oliver

Consider a web browser:

If you are at the site

[http://example.com/a](http://example.com/a)

and you click a link whose href is b, its relative and you go to  
[http://example.com/b](http://example.com/b)

If however the webs server lets you browse directories  
and you are at [http://example.com/dir/](http://example.com/dir/) and you click a link a, you’ll  
end up at

[http://example.com/dir/a](http://example.com/dir/a)

URL/URIs aren’t quite the same as file paths

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 10, 2006, 10:01am UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/5 "2006-04-10T10:01:22Z")

</div>

> Can you explain to me why this is correct? I would expect  
> [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) as the result of your call.

Sorry, I misread your original statement, which might make my reply  
confusing.

You are getting the correct results, as others have said.

It works just like in a browser:  
[http://example.com/one](http://example.com/one) + two =\> [http://example.com/two](http://example.com/two)  
[http://example.com/one/](http://example.com/one/) + two =\> [http://example.com/one/two](http://example.com/one/two)  
[http://example.com/one](http://example.com/one) + /two =\> [http://example.com/two](http://example.com/two)  
[http://example.com/one/](http://example.com/one/) + /two =\> [http://example.com/two](http://example.com/two)

Paul.

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 10, 2006, 1:02am UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/6 "2006-04-10T01:02:46Z")

</div>

On Apr 10, 2006, at 12:22 AM, Oliver A. wrote:

> Paul B. wrote:
> 
> > URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1)”, “test2”).to\_s #=\>  
> > “[test.de Archiv | Stiftung Warentest](http://test.de/test2)”
> 
> Can you explain to me why this is correct? I would expect  
> [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) as the result of your call.

Think of it this way –

You are in your web-browser on [test.de Archiv | Stiftung Warentest](http://test.de/test1). There is a link  
to “test2”:

```
<a href="test2">test2</a>

```

Would you expect it to go to [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2)? No, it would  
go to [test.de Archiv | Stiftung Warentest](http://test.de/test2).

You want:

URI.join(“[test.de Archiv | Stiftung Warentest](http://test.de/test1/)”, “test2”)  
^  
URI.join(“[http://test.de/](http://test.de/)”, “test1/”, “test2”)  
^

– Daniel

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [April 10, 2006, 10:12am UTC](https://www.ruby-forum.com/t/uri-join-broken/57327/7 "2006-04-10T10:12:23Z")

</div>

Paul B. wrote:

> > Can you explain to me why this is correct? I would expect  
> > [test.de Archiv | Stiftung Warentest](http://test.de/test1/test2) as the result of your call.
> 
> Sorry, I misread your original statement, which might make my reply  
> confusing.
> 
> You are getting the correct results, as others have said.
> 
> It works just like in a browser:  
> [http://example.com/one](http://example.com/one) + two =\> [http://example.com/two](http://example.com/two)  
> [http://example.com/one/](http://example.com/one/) + two =\> [http://example.com/one/two](http://example.com/one/two)  
> [http://example.com/one](http://example.com/one) + /two =\> [http://example.com/two](http://example.com/two)  
> [http://example.com/one/](http://example.com/one/) + /two =\> [http://example.com/two](http://example.com/two)

Thanks Guys,

now I know at least the logic behind URI.join. I am not sure, if it is  
the method of choice for my problem, but I have to think about it and  
may be I come back with another question.

Oliver
