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

ruby-changes:44010

From: ktsj <ko1@a...>
Date: Wed, 7 Sep 2016 15:06:13 +0900 (JST)
Subject: [ruby-changes:44010] ktsj:r56083 (trunk): * lib/csv.rb (CSV::{Row, Table}#{each, delete_if}): returns an enumerator

ktsj	2016-09-07 15:06:09 +0900 (Wed, 07 Sep 2016)

  New Revision: 56083

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56083

  Log:
    * lib/csv.rb (CSV::{Row,Table}#{each,delete_if}): returns an enumerator
      if no block is given. [ruby-core:75346] [Feature #11058]
    
    * test/csv/test_row.rb: add test for above.
    
    * test/csv/test_table.rb: ditto.

  Modified files:
    trunk/ChangeLog
    trunk/lib/csv.rb
    trunk/test/csv/test_row.rb
    trunk/test/csv/test_table.rb
Index: test/csv/test_row.rb
===================================================================
--- test/csv/test_row.rb	(revision 56082)
+++ test/csv/test_row.rb	(revision 56083)
@@ -209,9 +209,20 @@ class TestCSV::Row < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_row.rb#L209
     # by header
     assert_equal(["C", 3], @row.delete("C"))
 
-    # using a block
+  end
+
+  def test_delete_if
     assert_equal(@row, @row.delete_if { |h, f| h == "A" and not f.nil? })
-    assert_equal([["A", nil]], @row.to_a)
+    assert_equal([["B", 2], ["C", 3], ["A", nil]], @row.to_a)
+  end
+
+  def test_delete_if_without_block
+    enum = @row.delete_if
+    assert_instance_of(Enumerator, enum)
+    assert_equal(@row.size, enum.size)
+
+    assert_equal(@row, enum.each { |h, f| h == "A" and not f.nil? })
+    assert_equal([["B", 2], ["C", 3], ["A", nil]], @row.to_a)
   end
 
   def test_fields
@@ -281,6 +292,16 @@ class TestCSV::Row < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_row.rb#L292
 
     # verify that we can chain the call
     assert_equal(@row, @row.each { })
+
+    # without block
+    ary = @row.to_a
+    enum = @row.each
+    assert_instance_of(Enumerator, enum)
+    assert_equal(@row.size, enum.size)
+    enum.each do |pair|
+      assert_equal(ary.first.first, pair.first)
+      assert_equal(ary.shift.last, pair.last)
+    end
   end
 
   def test_enumerable
Index: test/csv/test_table.rb
===================================================================
--- test/csv/test_table.rb	(revision 56082)
+++ test/csv/test_table.rb	(revision 56083)
@@ -220,6 +220,17 @@ class TestCSV::Table < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_table.rb#L220
     # verify that we can chain the call
     assert_equal(@table, @table.each { })
 
+    # without block
+    enum = @table.each
+    assert_instance_of(Enumerator, enum)
+    assert_equal(@table.size, enum.size)
+
+    i = 0
+    enum.each do |row|
+      assert_equal(@rows[i], row)
+      i += 1
+    end
+
     ###################
     ### Column Mode ###
     ###################
@@ -231,6 +242,17 @@ class TestCSV::Table < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_table.rb#L242
       assert_equal(@table[header], column)
     end
 
+    # without block
+    enum = @table.each
+    assert_instance_of(Enumerator, enum)
+    assert_equal(@table.headers.size, enum.size)
+
+    headers = @table.headers
+    enum.each do |header, column|
+      assert_equal(headers.shift, header)
+      assert_equal(@table[header], column)
+    end
+
     ############################
     ### One Shot Mode Change ###
     ############################
@@ -363,6 +385,24 @@ class TestCSV::Table < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_table.rb#L385
     END_RESULT
   end
 
+  def test_delete_if_row_without_block
+    ######################
+    ### Mixed/Row Mode ###
+    ######################
+    enum = @table.delete_if
+    assert_instance_of(Enumerator, enum)
+    assert_equal(@table.size, enum.size)
+
+    # verify that we can chain the call
+    assert_equal(@table, enum.each { |row| (row["B"] % 2).zero? })
+
+    # verify resulting table
+    assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
+    A,B,C
+    4,5,6
+    END_RESULT
+  end
+
   def test_delete_if_column
     ###################
     ### Column Mode ###
@@ -373,6 +413,25 @@ class TestCSV::Table < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_table.rb#L413
     assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
     A
     1
+    4
+    7
+    END_RESULT
+  end
+
+  def test_delete_if_column_without_block
+    ###################
+    ### Column Mode ###
+    ###################
+    @table.by_col!
+
+    enum = @table.delete_if
+    assert_instance_of(Enumerator, enum)
+    assert_equal(@table.headers.size, enum.size)
+
+    assert_equal(@table, enum.each { |h, v| h > "A" })
+    assert_equal(<<-END_RESULT.gsub(/^\s+/, ""), @table.to_csv)
+    A
+    1
     4
     7
     END_RESULT
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56082)
+++ ChangeLog	(revision 56083)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Sep  7 14:56:59 2016  Kazuki Tsujimoto  <kazuki@c...>
+
+	* lib/csv.rb (CSV::{Row,Table}#{each,delete_if}): returns an enumerator
+	  if no block is given. [ruby-core:75346] [Feature #11058]
+
+	* test/csv/test_row.rb: add test for above.
+
+	* test/csv/test_table.rb: ditto.
+
 Wed Sep  7 14:50:01 2016  Kazuki Tsujimoto  <kazuki@c...>
 
 	* gems/bundled_gems: update to power_assert 0.3.1.
Index: lib/csv.rb
===================================================================
--- lib/csv.rb	(revision 56082)
+++ lib/csv.rb	(revision 56083)
@@ -431,7 +431,11 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L431
     #
     # This method returns the row for chaining.
     #
+    # If no block is given, an Enumerator is returned.
+    #
     def delete_if(&block)
+      block or return enum_for(__method__) { size }
+
       @row.delete_if(&block)
 
       self  # for chaining
@@ -500,13 +504,15 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L504
 
     #
     # Yields each pair of the row as header and field tuples (much like
-    # iterating over a Hash).
+    # iterating over a Hash). This method returns the row for chaining.
     #
-    # Support for Enumerable.
+    # If no block is given, an Enumerator is returned.
     #
-    # This method returns the row for chaining.
+    # Support for Enumerable.
     #
     def each(&block)
+      block or return enum_for(__method__) { size }
+
       @row.each(&block)
 
       self  # for chaining
@@ -822,7 +828,11 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L828
     #
     # This method returns the table for chaining.
     #
+    # If no block is given, an Enumerator is returned.
+    #
     def delete_if(&block)
+      block or return enum_for(__method__) { @mode == :row or @mode == :col_or_row ? size : headers.size }
+
       if @mode == :row or @mode == :col_or_row  # by index
         @table.delete_if(&block)
       else                                      # by header
@@ -845,7 +855,11 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L855
     #
     # This method returns the table for chaining.
     #
+    # If no block is given, an Enumerator is returned.
+    #
     def each(&block)
+      block or return enum_for(__method__) { @mode == :col ? headers.size : size }
+
       if @mode == :col
         headers.each { |header| block[[header, self[header]]] }
       else

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

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