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?我不得不想象以前有人为此编写了可靠的代码.
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获取一个字符串.
| 归档时间: |
|
| 查看次数: |
894 次 |
| 最近记录: |