# How to get an absolute address of a link

**URL:** <https://www.ruby-forum.com/t/how-to-get-an-absolute-address-of-a-link/158818>\
**Category:** Ruby\
**Created:** [January 28, 2009, 12:32pm UTC](https://www.ruby-forum.com/t/how-to-get-an-absolute-address-of-a-link/158818 "2009-01-28T12:32:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 12:32pm UTC](https://www.ruby-forum.com/t/how-to-get-an-absolute-address-of-a-link/158818/1 "2009-01-28T12:32:29Z")

</div>

Is there some URL lib that I can use to get the absolute address of a  
relative URL ? Maybe I could pass a “base” link to the method . Ideally  
, I would like to know if this could by achieved from mechanize , but  
anything would do .  
I wrote my own a while back , and I’m using it , but I would like to  
know if something already exists .

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-get-an-absolute-address-of-a-link/158818/2 "2009-01-28T17:28:35Z")

</div>

On Wed, Jan 28, 2009 at 9:30 AM, Tsunami S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there some URL lib that I can use to get the absolute address of a  
> relative URL ? Maybe I could pass a “base” link to the method . Ideally  
> , I would like to know if this could by achieved from mechanize , but  
> anything would do .

You can use URI.join

irb -ruri  
irb(main):001:0\> URI.join(‘[http://example.com](http://example.com)’, ‘/path/a/b’)  
=\> #\<URI::HTTP:0xb7ce2b08 URL:[http://example.com/path/a/b](http://example.com/path/a/b)\>  
irb(main):002:0\> URI.join(‘[http://example.com](http://example.com)’,  
‘[http://google.com/path/a/b](http://google.com/path/a/b)’)  
=\> #\<URI::HTTP:0xb7cdf28c URL:[http://google.com/path/a/b](http://google.com/path/a/b)\>  
irb(main):003:0\>

---

<div class="post-metadata">

**Author:** ![tsunami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tsunami](https://www.ruby-forum.com/u/tsunami)\
**Post date:** [January 28, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/how-to-get-an-absolute-address-of-a-link/158818/3 "2009-01-28T17:35:48Z")

</div>

Very nice ! Thanks ! I was wondering if it would handle joins with  
arguments like

arg1 = “[http://site.com](http://site.com)”  
arg2 = “index.html”

or arg1 = “[http://site.com/](http://site.com/)”  
arg2 = “/index.html”

and it seems to work great . Thank you !
