title: Reading Several URIs in ruby
body: <p>I need to read the contents of web page for several times and extract some information out of it for which I use regular expressions. I am using <code>open-uri</code> to read contents of the page and the sample code I written is as follows:</p>
  
  <pre><code>require 'open-uri'
  
  def getResults(words)
      results = []
      words.each do |word|
          results.push getAResult(word)
      end
      results
  end
  
  def getAResult(word)
      file = open("http://www.somapage.com?option=#{word}")
      contents = file.read
      file.close
      contents.match /some-regex-here/
      $1.empty? ? -1 : $1.to_f
  end
  </code></pre>
  
  <p>The problem is unless I comment out <code>file.close</code> line <code>getAResult</code> returns always <code>-1</code>. When I try this code on console, <code>getAResult</code> immediately returns <code>-1</code>, but ruby process runs for another two to three seconds or so.</p>
  
  <p>If I remove <code>file.close</code> line <code>getAResult</code> returns the correct result, but now <code>getResults</code> is a bunch of <code>-1</code>s except for the first one. I tried to use <code>curb</code> gem for reading the page, but similar problem appears.</p>
  
  <p>This seems like an issue related with threading. However, I couldn't come up with something reasonable to search and find a corresponding solution. What do you think problem would be?</p>
  
  <p>NOTE: This web page I try to read does not return results so fast. It takes some time.</p>