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/