0

In the below function (which is defined inside a class Myclass) I can run the function in ruby like

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

and the output of the calls gets printed to the terminal, but I the output is not assigned to the object myoutput.

is there a way to make the output return to an object, and not just print?

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
Roman C
  • 49,761
  • 33
  • 66
  • 176
sckott
  • 5,755
  • 2
  • 26
  • 42

2 Answers2

0

puts returns nil, so returning the results of puts won't do much for you.

Return results instead, or do both, e.g.,

def self.get_par(urls)
  # ... etc ...
  puts results
  results
end
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

Turns out I was just returning the output in the wrong place

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
sckott
  • 5,755
  • 2
  • 26
  • 42