【问题标题】:Ruby open_uri error NoMethodError: undefined method `strip' for 10:FixnumRuby open_uri 错误 NoMethodError: undefined method `strip' for 10:Fixnum
【发布时间】:2016-10-18 10:19:22
【问题描述】:

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

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'

这里有什么问题?

【问题讨论】:

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


    【解决方案1】:

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

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

    OpenURI 正在尝试对值运行 strip

    【讨论】:

      【解决方案2】:

      似乎它反对作为字符串的键。尝试将“read_timeout”变成一个符号:

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

      我可以使用“read_timeout”复制您的错误,但无论如何将其更改为修复它的符号。

      【讨论】:

        猜你喜欢
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 2015-03-19
        • 2016-08-13
        相关资源
        最近更新 更多