Ruby open_uri error NoMethodError: undefined method `strip' for 10:Fixnum

Ruby open_uri error NoMethodError: undefined method `strip' for 10:Fixnum

当我使用它时一切正常:

html = open("http://"+self.url).read

但是当我添加用户代理时:

html = open("http://"+self.url, "User-Agent" => "Ruby", 'read_timeout' => 10 ).read

我得到:

NoMethodError: undefined method `strip' for 10:Fixnum
    from /Users/a_user/.rbenv/versions/2.2.3/lib/ruby/2.2.0/net/http/header.rb:17:in `block in initialize_http_header'

这里有什么问题?

10 作为字符串而不是整数值传递

html = open("http://"+self.url, "User-Agent" => "Ruby", 'read_timeout' => '10' ).read

OpenURI is trying to run strip

它似乎反对键是字符串。试试把 'read_timeout' 变成一个符号:

html = open("http://"+self.url, "User-Agent" => "Ruby", read_timeout: 10 ).read

我可以用 'read_timeout' 复制你的错误,但无论如何将它更改为符号修复它。