Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby code to extract host from URL string

Tags:

ruby

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

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

like image 769
Michael Glenn Avatar asked Nov 10 '08 17:11

Michael Glenn


2 Answers

You could try something like this:

require 'uri'  myUri = URI.parse( 'http://www.mglenn.com/directory' ) print myUri.host # =>  www.mglenn.com 
like image 130
glenatron Avatar answered Oct 06 '22 08:10

glenatron


URI("http://www.mglenn.com/directory").host
like image 22
Kumar Avatar answered Oct 06 '22 06:10

Kumar