diff options
author | ktsj <ktsj@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-09-07 06:06:09 +0000 |
---|---|---|
committer | ktsj <ktsj@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-09-07 06:06:09 +0000 |
commit | b425d4f19ad9efaefcb1a767a6ea26e6d40e3985 (patch) | |
tree | 203abbebd3964eb0c682b1ba0a7ab88ee04a6f08 /lib/csv.rb | |
parent | f60caf13f9d2a54a7879c80b2df4829800798608 (diff) | |
download | ruby-b425d4f19ad9efaefcb1a767a6ea26e6d40e3985.tar.gz |
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/csv.rb')
-rw-r--r-- | lib/csv.rb | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/csv.rb b/lib/csv.rb index b68bcb0fc5..03b87983e6 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -431,7 +431,11 @@ class CSV # # 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 # # 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 # # 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 # # 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 |