From: "jwille (Jens Wille)" Date: 2013-09-20T17:48:43+09:00 Subject: [ruby-core:57288] [ruby-trunk - Bug #8929] CSV.foreach(filename) without block returns failing Enumerator Issue #8929 has been updated by jwille (Jens Wille). =begin why not simply return an Enumerator from `foreach`? class CSV def self.foreach(path, options = Hash.new, &block) if block_given? open(path, options) do |csv| csv.each(&block) end else to_enum(__method__, path, options) end end end which yields ary = []; CSV.foreach(filename) {|entry| ary << entry } ary == CSV.foreach(filename).to_a #=> true =end ---------------------------------------- Bug #8929: CSV.foreach(filename) without block returns failing Enumerator https://bugs.ruby-lang.org/issues/8929#change-41904 Author: martinjos (Martin Sidaway) Status: Open Priority: Normal Assignee: Category: Target version: ruby -v: 2.0.0-p247 Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN CSV.foreach(filename) {|entry| p entry } => works CSV.foreach(filename).to_a => fails It gives the following error: IOError: closed stream from /home/martin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1776:in `gets' from /home/martin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1776:in `block in shift' from /home/martin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1774:in `loop' from /home/martin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1774:in `shift' from /home/martin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1716:in `each' from (irb):7:in `each' from (irb):7:in `to_a' (...) -- http://bugs.ruby-lang.org/