[前][次][番号順一覧][スレッド一覧]

ruby-changes:31056

From: nobu <ko1@a...>
Date: Fri, 4 Oct 2013 09:32:43 +0900 (JST)
Subject: [ruby-changes:31056] nobu:r43135 (trunk): csv.rb: foreach enumerator

nobu	2013-10-04 09:32:35 +0900 (Fri, 04 Oct 2013)

  New Revision: 43135

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43135

  Log:
    csv.rb: foreach enumerator
    
    * lib/csv.rb (CSV.foreach): support enumerator.  based on a patch by
      Hanmac (Hans Mackowiak) at [ruby-core:57643].  [ruby-core:57283]
      [Feature #8929]

  Modified files:
    trunk/ChangeLog
    trunk/lib/csv.rb
    trunk/test/csv/test_interface.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43134)
+++ ChangeLog	(revision 43135)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Oct  4 09:32:33 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/csv.rb (CSV.foreach): support enumerator.  based on a patch by
+	  Hanmac (Hans Mackowiak) at [ruby-core:57643].  [ruby-core:57283]
+	  [Feature #8929]
+
 Thu Oct  3 18:20:47 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* win32/win32.c (console_emulator_p, constat_handle): disable built-in
Index: lib/csv.rb
===================================================================
--- lib/csv.rb	(revision 43134)
+++ lib/csv.rb	(revision 43135)
@@ -1116,6 +1116,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1116
   # but transcode it to UTF-8 before CSV parses it.
   #
   def self.foreach(path, options = Hash.new, &block)
+    return to_enum(__method__, path, options) unless block
     open(path, options) do |csv|
       csv.each(&block)
     end
Index: test/csv/test_interface.rb
===================================================================
--- test/csv/test_interface.rb	(revision 43134)
+++ test/csv/test_interface.rb	(revision 43135)
@@ -40,6 +40,12 @@ class TestCSV::Interface < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_interface.rb#L40
     end
   end
 
+  def test_foreach_enum
+    CSV.foreach(@path, col_sep: "\t", row_sep: "\r\n").zip(@expected) do |row, exp|
+      assert_equal(exp, row)
+    end
+  end
+
   def test_open_and_close
     csv = CSV.open(@path, "r+", col_sep: "\t", row_sep: "\r\n")
     assert_not_nil(csv)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]