0

在下面的函数(在一个类中定义Myclass)中,我可以在 ruby​​ 中运行该函数,如

myoutput =  Myclass.get_par("http://eol.org/api/ping/1.0.json,http://eol.org/api/ping/1.0.json")

并且调用的输出被打印到终端,但我的输出没有分配给 object myoutput

有没有办法让输出返回到一个对象,而不仅仅是打印?

def self.get_par(urls)
  allurls = urls.split(',')
  results = []
  EM.synchrony do
    concurrency = 2
    results << EM::Synchrony::Iterator.new(allurls, concurrency).map do |url, iter|
      http = EventMachine::HttpRequest.new(url).aget
      http.callback { iter.return(http.response) }
      http.errback { iter.return(http) }
    end
    EventMachine.stop
    puts results # all completed requests
  end
end
4

2 回答 2

0

putsReturns ,因此返回nil的结果puts对您没有太大帮助。

results改为返回,或者两者都做,例如,

def self.get_par(urls)
  # ... etc ...
  puts results
  results
end
于 2013-04-17T17:30:33.183 回答
0

原来我只是在错误的地方返回输出

def self.get_par(urls)
  allurls = urls.split(',')
  results = []
  EM.synchrony do
    concurrency = 2
    results << EM::Synchrony::Iterator.new(allurls, concurrency).map do |url, iter|
      http = EventMachine::HttpRequest.new(url).aget
      http.callback { iter.return(http.response) }
      http.errback { iter.return(http) }
    end
    EventMachine.stop
  end
  results # SHOULD HAVE BEEN HERE #
end
于 2013-04-17T17:38:12.747 回答