How can I set scheme to a URI object in Ruby

Viewed 6797

I'm trying to parse a URI from user input. I'm assuming some users won't put the scheme in their URI's and I want to default to "http".

The following code doesn't work:

require 'uri'   

uri_to_check = URI::parse("www.google.com")
uri_to_check.scheme = "http" unless uri_to_check.scheme

puts uri_to_check.to_s

I expect to see "http://www.google.com" but I get "http:www.google.com". Is it even possible to do it this way?

If so, what am I missing?

Is there a better way to do this?

2 Answers
Related