简体   繁体   中英

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

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 URI s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM