aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-04 00:32:35 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-04 00:32:35 +0000
commit28204c67ed6e55309cdf05e5e20f1f7e59e85e96 (patch)
tree458aa6709be867a48afdecc822ee20053f2f44ad
parent6c4387b4f2aa2e81bfd611fe5b5d9f67a2928b7b (diff)
downloadruby-28204c67ed6e55309cdf05e5e20f1f7e59e85e96.tar.gz
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43135 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--lib/csv.rb1
-rwxr-xr-xtest/csv/test_interface.rb6
3 files changed, 13 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index befeadb615..c1a01556bb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Fri Oct 4 09:32:33 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* win32/win32.c (console_emulator_p, constat_handle): disable built-in
diff --git a/lib/csv.rb b/lib/csv.rb
index b6d9121146..15f06c96f8 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -1116,6 +1116,7 @@ class CSV
# 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
diff --git a/test/csv/test_interface.rb b/test/csv/test_interface.rb
index 73e6ca9a4a..ad310fae49 100755
--- a/test/csv/test_interface.rb
+++ b/test/csv/test_interface.rb
@@ -40,6 +40,12 @@ class TestCSV::Interface < TestCSV
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)