Ahmed Taker
Ahmed Taker

Reputation: 403

Error in ruby While using require 'open-uri'

    open("http://www.ruby-lang.org/en") {|f|
  f.each_line {|line| p line}
  p f.base_uri         # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
  p f.content_type     # "text/html"
  p f.charset          # "iso-8859-1"
  p f.content_encoding # []
  p f.last_modified    # Thu Dec 05 02:45:02 UTC 2002
}

When i try to open the problem it gives me a error saying

C:/Users/Administrator/Desktop/ruby.rb:1:in `initialize': Invalid argument - htt
p://www.ruby-lang.org/en (Errno::EINVAL)
        from C:/Users/Administrator/Desktop/ruby.rb:1:in `open'
        from C:/Users/Administrator/Desktop/ruby.rb:1:in `<main>'

What does that error mean and how to fix it and thanks

Upvotes: 0

Views: 685

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

By default open is Kernel#open method which knows nothing about http proto.

To make your code working you should explicitely

require 'open-uri'

which monkeypatches open method so that it now may open URIs.

Upvotes: 4

Related Questions