Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `hostname' for "'http://www.google.com/":String (NoMethodError)

I have the following code to get google.com

class Geocoder
    def self.locate()

        uri="http://www.google.com/"



        puts Net::HTTP.get(uri)

end

but i faced with the erorr:

undefined method `hostname' for "'http://www.google.com/":String (NoMethodError)

I have already seen this, and my ruby version is: ruby 2.2.1

like image 708
Sal-laS Avatar asked Nov 20 '15 08:11

Sal-laS


1 Answers

You're missing parsing the string into a URI... Here is what it should look like:

class Geocoder
  def self.locate(address)
    escaped_address = URI.escape(address) 
    uri = URI.parse(escaped_address)
    puts Net::HTTP.get(uri)
  end
end
like image 148
Brad Werth Avatar answered Oct 31 '22 14:10

Brad Werth