如何检索通用URL的'scheme:// domain'部分?

时间:2011-09-05 05:03:27

标签: ruby-on-rails ruby ruby-on-rails-3 url

我正在使用Ruby on Rails 3.0.10,我想检索通用URL的scheme://domain部分(注意:URL语法也可以是scheme://domain:port/path?query_string#fragment_id)。

也就是说,例如,如果我有以下网址

http://stackoverflow.com/questions/7304043/how-to-retrieve-the-scheme-domainport-part-of-a-generic-url
ftp://some_link.org/some_other_string

# Consider also 'https', 'ftps' and so on... 'scheme:' values.

我想只检索

# Note: The last '/' character was removed.

http://stackoverflow.com
ftp://some_link.org

# Consider also 'https', 'ftps' and so on... 'scheme:' values.

份。 我该怎么做?

1 个答案:

答案 0 :(得分:5)

require 'uri'

uri = URI.parse("http://stackoverflow.com/questions/7304043/how-to-retrieve-the-scheme-domainport-part-of-a-generic-url")
url = "#{uri.scheme}://#{uri.host}" #url would be set to http://stackoverflow.com

来自Module: URI

相关问题