Ruby(或Rails):如何将相对URL应用于完整URL?

Ala*_* H. 2 ruby ruby-on-rails ruby-on-rails-3

给定一个完整的URL,我需要处理所有可能的有效相对URL,就像您在第一个位置的页面上的HREF属性或Location:标题中看到的那样.

例子:

完成:  http://example.com/foo/bar.html
输入: calico.gif
结果: http://example.com/foo/calico.gif

完成:  http://example.com/foo/bar.html
输入: ../x.html
结果: http://example.com/x.html

完成:  http://example.com/foo/bar.html
输入: /robots.txt
结果: http://example.com/robots.txt

如何可靠地处理这些相对URL?我不得不想象以前有人为此编写了可靠的代码.

Rob*_*bin 5

require 'uri'
URI::join("http://domain.com/dir", "/images/small/image.png")
URI::join("http://example.com/foo/bar.html", "calico.gif")
...
Run Code Online (Sandbox Code Playgroud)

并只是调用.to_s获取一个字符串.