title: Ruby 1.9.2 - Read and parse a remote CSV
body: <p>I am looking for a way to read and parse locally a remote CSV (hosted on a particular website).</p>
  
  <p>I found on the Internet a couple of interesting examples that make use of FasterCSV, that in ruby 1.9.2 has been merged into CSV. I found that you can read a remote CSV using the gems 'csv' and 'open-uri' this way:</p>
  
  <pre><code>require 'csv'
  require 'open-uri'
  
  def read(url)
    open(url) do |f|
      f.each_line do |l|
        CSV.parse(l) do |row|
          puts row
        end
      end
    end
  end
  </code></pre>
  
  <p>But when I call this function, I get an exception:</p>
  
  <pre><code>ERROR IOError: closed stream
  </code></pre>
  
  <p>Anyone can explain me why? Is there anything wrong? Should I choose another approach for reading remote CSV's?</p>
  
  <h3>Update</h3>
  
  <p>The best solution I've found till now is this:</p>
  
  <pre><code>def read(url)
    data = []
    begin
      open(url) do |f|
        data = CSV.parse f
      end
    rescue IOError =&gt; e
      # Silently catch the exception ...
    end
  
    return data
  end
  </code></pre>
  
  <p>but it somewhat seems not so clean. I really do not like silently catching an exception where it shouldn't be ...</p>
  
  <h3>Update 2</h3>
  
  <p>I can reproduce the error using both</p>
  
  <pre><code>ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
  </code></pre>
  
  <p>This is the code from my <code>test.rb</code> file:</p>
  
  <pre><code>require 'rubygems'
  require 'open-uri'
  require 'csv'
  
  def read(url)
    data = []
    begin
      open(url) do |f|
        data = CSV.parse f
      end
    end
  
    puts data
  end
  
  read("http://www.euribor-ebf.eu/assets/modules/rateisblue/processed_files/myav_EURIBOR_2011.csv")
  </code></pre>
  
  <p>And this is the output of the <code>ruby test.rb</code> command</p>
  
  <pre><code>/Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in `close': closed stream (IOError)
  from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:152:in `open_uri'
  from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:671:in `open'
  from /Users/marzu/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/open-uri.rb:33:in `open'
  from test.rb:8:in `read'
  from test.rb:16:in `&lt;main&gt;'
  </code></pre>
  
  <p>I am using <code>rvm 1.6.9</code> on Mac OS X 10.6.7.</p>
  
  <p>Any suggestions?</p>