From: Rob Biedenharn Date: 2009-09-28T13:11:20+09:00 Subject: Re: Getting a valid URL from a command line On Sep 27, 2009, at 11:48 PM, Hunt Jon wrote: > Hi - I'm working on the script below, which attempts at getting > a user input and validate that the input is formed like a URL. > And if the user fails to input, it should ask again. > > require 'uri' > puts "Type a URL" > begin > url = gets.chomp > URI.parse(url) # should raise if a variable 'url' is malformed. > rescue URI::InvalidURIError > puts "That is not a valid URL. Try again." > retry > end > > I expect that if I run "URI.parse()" it should raise an error, but > it doesn't happen. > > Can anybody help me on this one? > > Jon require 'uri' print "Type a URL: " begin url = gets.chomp puts "You said: #{url.inspect}" uri = URI.parse(url) # should raise if a variable 'url' is malformed. puts uri.inspect rescue URI::InvalidURIError puts "That is not a valid URL. Try again." retry end Try getting a little bit more information out (and post what input you are trying that you expect to be malformed). Note that some URI's are HTTP and some might be Generic. There are a lot more types of URI that just those that start with http://. Have you ever seen a jdbc resource string? -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com