Ruby代码从URL字符串中提取主机

时间:2022-06-28 16:04:15

I'm looking for a method to reliably extract the host name from a URL string in Ruby.

我正在寻找一种从Ruby中的URL字符串中可靠地提取主机名的方法。

e.g. http://www.mglenn.com/directory = www.mglenn.com OR http://www.mglenn.com?param=x = www.mglenn.com

例如http://www.mglenn.com/directory = www.mglenn.com或http://www.mglenn.com?param=x = www.mglenn.com

2 个解决方案

#1


62  

You could try something like this:

你可以尝试这样的事情:

require 'uri'

myUri = URI.parse( 'http://www.mglenn.com/directory' )
print myUri.host
# =>  www.mglenn.com

#2


20  

URI("http://www.mglenn.com/directory").host

#1


62  

You could try something like this:

你可以尝试这样的事情:

require 'uri'

myUri = URI.parse( 'http://www.mglenn.com/directory' )
print myUri.host
# =>  www.mglenn.com

#2


20  

URI("http://www.mglenn.com/directory").host