From: "Hanmac (Hans Mackowiak)" Date: 2013-10-04T02:13:27+09:00 Subject: [ruby-core:57643] [ruby-trunk - Bug #8929] CSV.foreach(filename) without block returns failing Enumerator Issue #8929 has been updated by Hanmac (Hans Mackowiak). another sample where the difference is shown: CSV.foreach('test.csv').with_index { |csv,i| p i } #<< fails CSV.to_enum(:foreach,'test.csv').with_index { |csv,i| p i } # works i think the code from jwillie would be nearly the best, but i would use this: return to_enum(__method__, path, options) unless block_given? ---------------------------------------- Bug #8929: CSV.foreach(filename) without block returns failing Enumerator https://bugs.ruby-lang.org/issues/8929#change-42259 Author: martinjos (Martin Sidaway) Status: Feedback Priority: Normal Assignee: JEG2 (James Gray) Category: lib 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/