我正在寻找一种从Ruby中的URL字符串中可靠地提取主机名的方法.
例如 http://www.mglenn.com/directory = www.mglenn.com或 http://www.mglenn.com?param=x = www.mglenn.com
你可以尝试这样的事情:
require 'uri' myUri = URI.parse( 'http://www.mglenn.com/directory' ) print myUri.host # => www.mglenn.com
URI("http://www.mglenn.com/directory").host