Ruby open_uri错误NoMethodError:10的未定义方法`strip':Fixnum

时间:2016-10-18 10:19:22

标签: ruby-on-rails ruby open-uri

当我使用它时,一切都很好:

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'

这里有什么问题?

2 个答案:

答案 0 :(得分:3)

10作为字符串而非整数值

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

OpenURI 正在尝试对值strip运行

答案 1 :(得分:-1)

它似乎反对键是一个字符串。尝试转动' read_timeout'成为一个符号:

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

我可以使用' read_timeout'来复制您的错误。但无论如何,把它改成一个符号固定在这里。