Ruby 的 open-uri open 方法工作



这是一个奇怪的情况,希望你能帮我一个理由:

require "open-uri"
class TraceParser
  def starttorip
    url = 'http://yahoo.com'
    proxy_addr = 'http://my proxy server:'
    proxy_port = 1010
    begin
      open(url, :proxy => (proxy_addr + proxy_port.to_s)) do |source|
        source.each_line do |x|
          puts x
        end
      end
    end
  end
  varb = TraceParser.new
  varb.starttorip
end

上面的代码就像一个魅力,一切都是美好的。但是,当我将代理定义更改为如下所示时:

require "open-uri"
class TraceParser
  def starttorip
    url = 'http://yahoo.com'
    begin
      open (url, :proxy => 'http://my proxy server:1010') do |source|
        source.each_line do |x|
          puts x
        end
      end
    end
  end
  varb = TraceParser.new
  varb.starttorip
end

一切都松动了:

/tracerparser.rb:6: syntax error, unexpected ',', expecting ')' (SyntaxError)
      open (url, :proxy => "my proxy server...

据我了解,第一个是将代理 defn 拆分为两个字符串并附加到打开的宝石中。感谢您分享您的意见。

问题是open后面的空间,它与代理映射条目无关。

最新更新