csv 3.3.5 → 3.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f77fe04292e17f436f9c5838bb3cbd6aa4297b807478c127321323fc4ad27c28
4
- data.tar.gz: 63ecf6ce80dcfa3b7eb24d0f3947455eb223d1c44a32b58b4d084b29efad8961
3
+ metadata.gz: 405d44be299fb5493de237683e24cdbf6e29982e4378c1d01a82e2ad4bbb21a3
4
+ data.tar.gz: 28a53de42e2c35091f441a3d9a7a886afc30bad1c514ea26715d633ee8a00080
5
5
  SHA512:
6
- metadata.gz: f9ffbf24700b4ab0eb35a2cb8086e82e36825558c14b646ae00374cb3e80f7195ebb3c9ac08d11b011ffa0cc10ae607a79e24d68b793b54c50d2dd1e8bd5ec16
7
- data.tar.gz: 628a0b0c0963d69686845172d84fbbdf1cebb9fc6839e052054f9e7c112bbcb96743a63e8cf5d1a6df8a9299f03ab3b8d81a9b814fb6d0f222e444b9f0282a5d
6
+ metadata.gz: 7cb81c8caf90a7673fd22fc24644b33fb171c9f7b60ea9e679612008912d7ae10150351001d2daea5afdc6360cbe486f454cc3b29387b9c514d232b0c68b5ad3
7
+ data.tar.gz: 72d77e22c654b26dc8fed349670d107169605119554f6b18cd79f7477bd7284f099560edc68bfba1561775fe8acb081fd1427aa6235c8540bc7cca93a7e7a397
data/NEWS.md CHANGED
@@ -1,5 +1,62 @@
1
1
  # News
2
2
 
3
+ ## 3.3.7 - 2026-10-28
4
+
5
+ ### Improvements
6
+
7
+ * Improved documents.
8
+ * GH-376
9
+ * Patch by Oskar Eichler
10
+
11
+ * Improved error handling.
12
+ * GH-378
13
+ * GH-379
14
+ * Reported by sswander
15
+
16
+ ### Thanks
17
+
18
+ * Oskar Eichler
19
+
20
+ * sswander
21
+
22
+ ## 3.3.6 - 2026-07-27
23
+
24
+ ### Improvements
25
+
26
+ * `CSV::Row#to_h`: Added support for block.
27
+ * GH-356
28
+ * Patch by Vlad
29
+
30
+ * Ensured using `path_or_io` for parameter name that accepts path or
31
+ IO.
32
+ * GH-358
33
+ * Patch by Yuto Urushima
34
+
35
+ * Changed to not using enumerator in `CSV`.
36
+ * GH-361
37
+ * GH-363
38
+ * Reported by Cas Donoghue
39
+
40
+ ### Fixes
41
+
42
+ * Fixed a bug that `\r` in unquoted fields are rejected when row
43
+ separator doesn't contain `\r`
44
+ * GH-60
45
+ * GH-346
46
+ * Patch by Jas
47
+
48
+ * Fixed a typo in documentation.
49
+ * GH-350
50
+ * Patch by tmr111116
51
+
52
+ ### Thanks
53
+
54
+ * Jas
55
+ * tmr111116
56
+ * Vlad
57
+ * Yuto Urushima
58
+ * Cas Donoghue
59
+
3
60
  ## 3.3.5 - 2025-06-01
4
61
 
5
62
  ### Improvements
@@ -10,7 +67,7 @@
10
67
 
11
68
  ### Thanks
12
69
 
13
- * Petrik de Heus
70
+ * Petrik de Heus
14
71
 
15
72
  ## 3.3.4 - 2025-04-13
16
73
 
data/README.md CHANGED
@@ -52,4 +52,4 @@ Please do not submit issues and PRs that aim to introduce RuboCop in this reposi
52
52
 
53
53
  The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
54
54
 
55
- See LICENSE.txt for details.
55
+ See [LICENSE.txt](LICENSE.txt) for details.
@@ -1,6 +1,6 @@
1
1
  ====== Option +quote_char+
2
2
 
3
- Specifies the character (\String of length 1) used used to quote fields
3
+ Specifies the character (\String of length 1) used to quote fields
4
4
  in both parsing and generating.
5
5
  This String will be transcoded into the data's \Encoding before use.
6
6
 
@@ -17,7 +17,7 @@ Without +write_headers+:
17
17
  csv.shift
18
18
  end # => ["foo", "0"]
19
19
 
20
- With +write_headers+":
20
+ With +write_headers+:
21
21
  CSV.open(file_path,'w',
22
22
  :write_headers => true,
23
23
  :headers => ['Name','Value']
@@ -31,9 +31,10 @@ the first row of the data is treated as a row of headers:
31
31
  bax,2
32
32
  EOT
33
33
  csv = CSV.new(str, headers: true)
34
- csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:2 col_sep:"," row_sep:"\n" quote_char:"\"" headers:["Name", "Count"]>
34
+ csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"" headers:true>
35
+ csv.headers # => true
36
+ csv.shift # => #<CSV::Row "Name":"foo" "Count":"0">
35
37
  csv.headers # => ["Name", "Count"]
36
- csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
37
38
 
38
39
  ---
39
40
 
@@ -46,7 +47,7 @@ If set to an \Array, the \Array elements are treated as headers:
46
47
  csv = CSV.new(str, headers: ['Name', 'Count'])
47
48
  csv
48
49
  csv.headers # => ["Name", "Count"]
49
- csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
50
+ csv.shift # => #<CSV::Row "Name":"foo" "Count":"0">
50
51
 
51
52
  ---
52
53
 
@@ -60,4 +61,4 @@ with the current +options+, and the returned \Array is treated as headers:
60
61
  csv = CSV.new(str, headers: 'Name,Count')
61
62
  csv
62
63
  csv.headers # => ["Name", "Count"]
63
- csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
64
+ csv.shift # => #<CSV::Row "Name":"foo" "Count":"0">
@@ -6,7 +6,7 @@ a line that contains a column separator is not considered to be blank.
6
6
  Default value:
7
7
  CSV::DEFAULT_OPTIONS.fetch(:skip_blanks) # => false
8
8
 
9
- See also option {skiplines}[#class-CSV-label-Option+skip_lines].
9
+ See also option {skip_lines}[#class-CSV-label-Option+skip_lines].
10
10
 
11
11
  For examples in this section:
12
12
  str = <<-EOT
@@ -2,8 +2,9 @@
2
2
 
3
3
  Specifies an object to use in identifying comment lines in the input that are to be ignored:
4
4
  * If a \Regexp, ignores lines that match it.
5
- * If a \String, converts it to a \Regexp, ignores lines that match it.
5
+ * If a \String, ignores lines that include it.
6
6
  * If +nil+, no lines are considered to be comments.
7
+ * Otherwise, the object must respond to +match+; ignores lines for which +match+ returns a truthy value.
7
8
 
8
9
  Default value:
9
10
  CSV::DEFAULT_OPTIONS.fetch(:skip_lines) # => nil
@@ -32,6 +33,7 @@ Using a \String:
32
33
 
33
34
  ---
34
35
 
35
- Raises an exception if given an object that is not a \Regexp, a \String, or +nil+:
36
+ Raises an exception if given an object that is not a \Regexp, a \String, +nil+,
37
+ or an object that responds to +match+:
36
38
  # Raises ArgumentError (:skip_lines has to respond to #match: 0)
37
39
  CSV.parse(str, skip_lines: 0)
@@ -16,7 +16,7 @@ All code snippets on this page assume that the following has been executed:
16
16
  - {Generating to a File}[#label-Generating+to+a+File]
17
17
  - {Recipe: Generate to File with Headers}[#label-Recipe-3A+Generate+to+File+with+Headers]
18
18
  - {Recipe: Generate to File Without Headers}[#label-Recipe-3A+Generate+to+File+Without+Headers]
19
- - {Generating to IO an Stream}[#label-Generating+to+an+IO+Stream]
19
+ - {Generating to an IO Stream}[#label-Generating+to+an+IO+Stream]
20
20
  - {Recipe: Generate to IO Stream with Headers}[#label-Recipe-3A+Generate+to+IO+Stream+with+Headers]
21
21
  - {Recipe: Generate to IO Stream Without Headers}[#label-Recipe-3A+Generate+to+IO+Stream+Without+Headers]
22
22
  - {Converting Fields}[#label-Converting+Fields]
@@ -71,11 +71,11 @@ that are to be generated:
71
71
 
72
72
  ==== Generating to a \File
73
73
 
74
- You can generate /CSV data to a \File, with or without headers.
74
+ You can generate \CSV data to a \File, with or without headers.
75
75
 
76
76
  ===== Recipe: Generate to \File with Headers
77
77
 
78
- Use class method CSV.open with option +headers+ generate to a \File.
78
+ Use class method CSV.open with option +headers+ to generate to a \File.
79
79
 
80
80
  This example uses method CSV#<< to append the rows
81
81
  that are to be generated:
@@ -105,7 +105,7 @@ that are to be generated:
105
105
 
106
106
  You can generate \CSV data to an \IO stream, with or without headers.
107
107
 
108
- ==== Recipe: Generate to \IO Stream with Headers
108
+ ===== Recipe: Generate to \IO Stream with Headers
109
109
 
110
110
  Use class method CSV.new with option +headers+ to generate \CSV data to an \IO stream:
111
111
  path = 't.csv'
@@ -187,7 +187,7 @@ For strict compliance, use option +:row_sep+ to specify row separator <tt>"\r\n"
187
187
  ===== Recipe: Generate Non-Compliant Row Separator
188
188
 
189
189
  For data with non-compliant row separators, use option +:row_sep+ with a different value:
190
- This example source uses semicolon (<tt>";'</tt>) as its row separator:
190
+ This example source uses semicolon (<tt>";"</tt>) as its row separator:
191
191
  output_string = CSV.generate('', row_sep: ";") do |csv|
192
192
  csv << ['Foo', 0]
193
193
  csv << ['Bar', 1]
@@ -223,11 +223,11 @@ This example source uses TAB (<tt>"\t"</tt>) as its column separator:
223
223
 
224
224
  ==== Quotes
225
225
 
226
- IFC 4180 allows most fields to be quoted or not.
226
+ RFC 4180 allows most fields to be quoted or not.
227
227
  By default, \CSV does not quote most fields.
228
228
 
229
229
  However, a field containing the current row separator, column separator,
230
- or quote character is automatically quoted, producing IFC 4180 compliance:
230
+ or quote character is automatically quoted, producing RFC 4180 compliance:
231
231
  # Field contains row separator.
232
232
  output_string = CSV.generate('') do |csv|
233
233
  row_sep = csv.row_sep
data/lib/csv/parser.rb CHANGED
@@ -181,7 +181,7 @@ class CSV
181
181
  end
182
182
 
183
183
  def eos?
184
- @scanner.eos?
184
+ @last_scanner and @scanner.eos?
185
185
  end
186
186
 
187
187
  def keep_start
@@ -444,6 +444,11 @@ class CSV
444
444
  end
445
445
  end
446
446
 
447
+ def eof?
448
+ return false if @scanner.nil?
449
+ @scanner.eos?
450
+ end
451
+
447
452
  def use_headers?
448
453
  @use_headers
449
454
  end
@@ -675,7 +680,7 @@ class CSV
675
680
  def prepare_unquoted
676
681
  return if @quote_character.nil?
677
682
 
678
- no_unquoted_values = "\r\n".encode(@encoding)
683
+ no_unquoted_values = Regexp.escape(@row_separator).encode(@encoding)
679
684
  no_unquoted_values << @escaped_first_column_separator
680
685
  unless @liberal_parsing
681
686
  no_unquoted_values << @escaped_quote_character
@@ -973,63 +978,69 @@ class CSV
973
978
 
974
979
  def parse_quotable_loose(&block)
975
980
  @scanner.keep_start
976
- @scanner.each_line(@row_separator) do |line|
977
- if @skip_lines and skip_line?(line)
978
- @scanner.keep_drop
979
- @scanner.keep_start
980
- next
981
- end
982
- original_line = line
983
- line = line.delete_suffix(@row_separator)
984
-
985
- if line.empty?
986
- if @skip_blanks
981
+ kept = true
982
+ begin
983
+ @scanner.each_line(@row_separator) do |line|
984
+ if @skip_lines and skip_line?(line)
987
985
  @scanner.keep_drop
988
986
  @scanner.keep_start
989
987
  next
990
988
  end
991
- row = []
992
- quoted_fields = FieldsConverter::NO_QUOTED_FIELDS
993
- elsif line.include?(@cr) or line.include?(@lf)
994
- @scanner.keep_back
995
- @parse_method = :parse_quotable_robust
996
- return parse_quotable_robust(&block)
997
- else
998
- row = line.split(@split_column_separator, -1)
999
- quoted_fields = []
1000
- n_columns = row.size
1001
- i = 0
1002
- while i < n_columns
1003
- column = row[i]
1004
- if column.empty?
1005
- quoted_fields << false
1006
- row[i] = nil
1007
- else
1008
- n_quotes = column.count(@quote_character)
1009
- if n_quotes.zero?
989
+ original_line = line
990
+ line = line.delete_suffix(@row_separator)
991
+
992
+ if line.empty?
993
+ if @skip_blanks
994
+ @scanner.keep_drop
995
+ @scanner.keep_start
996
+ next
997
+ end
998
+ row = []
999
+ quoted_fields = FieldsConverter::NO_QUOTED_FIELDS
1000
+ elsif line.include?(@cr) or line.include?(@lf)
1001
+ kept = false
1002
+ @scanner.keep_back
1003
+ @parse_method = :parse_quotable_robust
1004
+ return parse_quotable_robust(&block)
1005
+ else
1006
+ row = line.split(@split_column_separator, -1)
1007
+ quoted_fields = []
1008
+ n_columns = row.size
1009
+ i = 0
1010
+ while i < n_columns
1011
+ column = row[i]
1012
+ if column.empty?
1010
1013
  quoted_fields << false
1011
- # no quote
1012
- elsif n_quotes == 2 and
1013
- column.start_with?(@quote_character) and
1014
- column.end_with?(@quote_character)
1015
- quoted_fields << true
1016
- row[i] = column[1..-2]
1014
+ row[i] = nil
1017
1015
  else
1018
- @scanner.keep_back
1019
- @parse_method = :parse_quotable_robust
1020
- return parse_quotable_robust(&block)
1016
+ n_quotes = column.count(@quote_character)
1017
+ if n_quotes.zero?
1018
+ quoted_fields << false
1019
+ # no quote
1020
+ elsif n_quotes == 2 and
1021
+ column.start_with?(@quote_character) and
1022
+ column.end_with?(@quote_character)
1023
+ quoted_fields << true
1024
+ row[i] = column[1..-2]
1025
+ else
1026
+ kept = false
1027
+ @scanner.keep_back
1028
+ @parse_method = :parse_quotable_robust
1029
+ return parse_quotable_robust(&block)
1030
+ end
1031
+ validate_field_size(row[i])
1021
1032
  end
1022
- validate_field_size(row[i])
1033
+ i += 1
1023
1034
  end
1024
- i += 1
1025
1035
  end
1036
+ @scanner.keep_drop
1037
+ @scanner.keep_start
1038
+ @last_line = original_line
1039
+ emit_row(row, quoted_fields, &block)
1026
1040
  end
1027
- @scanner.keep_drop
1028
- @scanner.keep_start
1029
- @last_line = original_line
1030
- emit_row(row, quoted_fields, &block)
1041
+ ensure
1042
+ @scanner.keep_drop if kept
1031
1043
  end
1032
- @scanner.keep_drop
1033
1044
  end
1034
1045
 
1035
1046
  def parse_quotable_robust(&block)
data/lib/csv/row.rb CHANGED
@@ -637,6 +637,7 @@ class CSV
637
637
 
638
638
  # :call-seq:
639
639
  # row.to_h -> hash
640
+ # row.to_h {|key, value| ... } -> hash
640
641
  #
641
642
  # Returns the new \Hash formed by adding each header-value pair in +self+
642
643
  # as a key-value pair in the \Hash.
@@ -650,11 +651,34 @@ class CSV
650
651
  # table = CSV.parse(source, headers: true)
651
652
  # row = table[0]
652
653
  # row.to_h # => {"Name"=>"Foo"}
654
+ #
655
+ # If a block is given, will call it with (key, value) arguments and use result as a hash entry:
656
+ # source = "Name,Value\nfoo,1\nbar,2\nbaz,3\n"
657
+ # table = CSV.parse(source, headers: true)
658
+ # row = table[0]
659
+ # row.to_h { |key, value| [key, "#{key}-#{value}"] } # => {"Name"=>"Name-foo", "Value"=>"Value-1"}
653
660
  def to_h
654
661
  hash = {}
655
- each do |key, _value|
656
- hash[key] = self[key] unless hash.key?(key)
662
+
663
+ if block_given?
664
+ each do |key, _value|
665
+ result = yield(key, self[key])
666
+ result_array = Array.try_convert(result)
667
+ raise TypeError, "wrong element type #{result.class} (expected array)" if result_array.nil?
668
+ raise ArgumentError, "wrong array length (expected 2, was #{result_array.size})" unless result_array.size == 2
669
+
670
+ key, value = result_array
671
+ next if hash.key?(key)
672
+
673
+ key.freeze if key.is_a?(String) && !key.frozen?
674
+ hash[key] = value
675
+ end
676
+ else
677
+ each do |key, _value|
678
+ hash[key] = self[key] unless hash.key?(key)
679
+ end
657
680
  end
681
+
658
682
  hash
659
683
  end
660
684
  alias_method :to_hash, :to_h
data/lib/csv/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  class CSV
4
4
  # The version of the installed library.
5
- VERSION = "3.3.5"
5
+ VERSION = "3.3.7"
6
6
  end
data/lib/csv.rb CHANGED
@@ -1377,7 +1377,7 @@ class CSV
1377
1377
  # * Arguments <tt>**options</tt> must be keyword options.
1378
1378
  # See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
1379
1379
  # * This method optionally accepts an additional <tt>:encoding</tt> option
1380
- # that you can use to specify the Encoding of the data read from +path+ or +io+.
1380
+ # that you can use to specify the Encoding of the data read from +path_or_io+.
1381
1381
  # You must provide this unless your data is in the encoding
1382
1382
  # given by <tt>Encoding::default_external</tt>.
1383
1383
  # Parsing will use this to determine how to parse the data.
@@ -1386,9 +1386,9 @@ class CSV
1386
1386
  # encoding: 'UTF-32BE:UTF-8'
1387
1387
  # would read +UTF-32BE+ data from the file
1388
1388
  # but transcode it to +UTF-8+ before parsing.
1389
- def foreach(path, mode="r", **options, &block)
1390
- return to_enum(__method__, path, mode, **options) unless block_given?
1391
- open(path, mode, **options) do |csv|
1389
+ def foreach(path_or_io, mode="r", **options, &block)
1390
+ return to_enum(__method__, path_or_io, mode, **options) unless block_given?
1391
+ open(path_or_io, mode, **options) do |csv|
1392
1392
  csv.each(&block)
1393
1393
  end
1394
1394
  end
@@ -1582,7 +1582,7 @@ class CSV
1582
1582
  # * Arguments <tt>**options</tt> must be keyword options.
1583
1583
  # See {Options for Generating}[#class-CSV-label-Options+for+Generating].
1584
1584
  # * This method optionally accepts an additional <tt>:encoding</tt> option
1585
- # that you can use to specify the Encoding of the data read from +path+ or +io+.
1585
+ # that you can use to specify the Encoding of the data read from +path_or_io+.
1586
1586
  # You must provide this unless your data is in the encoding
1587
1587
  # given by <tt>Encoding::default_external</tt>.
1588
1588
  # Parsing will use this to determine how to parse the data.
@@ -1644,11 +1644,11 @@ class CSV
1644
1644
  # Raises an exception if the argument is not a \String object or \IO object:
1645
1645
  # # Raises TypeError (no implicit conversion of Symbol into String)
1646
1646
  # CSV.open(:foo)
1647
- def open(filename_or_io, mode="r", **options)
1647
+ def open(path_or_io, mode="r", **options)
1648
1648
  # wrap a File opened with the remaining +args+ with no newline
1649
1649
  # decorator
1650
1650
  file_opts = {}
1651
- may_enable_bom_detection_automatically(filename_or_io,
1651
+ may_enable_bom_detection_automatically(path_or_io,
1652
1652
  mode,
1653
1653
  options,
1654
1654
  file_opts)
@@ -1661,11 +1661,11 @@ class CSV
1661
1661
  options.delete(:replace)
1662
1662
  options.delete_if {|k, _| /newline\z/.match?(k)}
1663
1663
 
1664
- if filename_or_io.is_a?(StringIO)
1665
- f = create_stringio(filename_or_io.string, mode, **file_opts)
1664
+ if path_or_io.is_a?(StringIO)
1665
+ f = create_stringio(path_or_io.string, mode, **file_opts)
1666
1666
  else
1667
1667
  begin
1668
- f = File.open(filename_or_io, mode, **file_opts)
1668
+ f = File.open(path_or_io, mode, **file_opts)
1669
1669
  rescue ArgumentError => e
1670
1670
  raise unless /needs binmode/.match?(e.message) and mode == "r"
1671
1671
  mode = "rb"
@@ -1901,10 +1901,10 @@ class CSV
1901
1901
 
1902
1902
  #
1903
1903
  # :call-seq:
1904
- # read(source, **options) -> array_of_arrays
1905
- # read(source, headers: true, **options) -> csv_table
1904
+ # read(path_or_io, **options) -> array_of_arrays
1905
+ # read(path_or_io, headers: true, **options) -> csv_table
1906
1906
  #
1907
- # Opens the given +source+ with the given +options+ (see CSV.open),
1907
+ # Opens the given +path_or_io+ with the given +options+ (see CSV.open),
1908
1908
  # reads the source (see CSV#read), and returns the result,
1909
1909
  # which will be either an \Array of Arrays or a CSV::Table.
1910
1910
  #
@@ -1919,22 +1919,22 @@ class CSV
1919
1919
  # path = 't.csv'
1920
1920
  # File.write(path, string)
1921
1921
  # CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
1922
- def read(path, **options)
1923
- open(path, **options) { |csv| csv.read }
1922
+ def read(path_or_io, **options)
1923
+ open(path_or_io, **options) { |csv| csv.read }
1924
1924
  end
1925
1925
 
1926
1926
  # :call-seq:
1927
- # CSV.readlines(source, **options)
1927
+ # CSV.readlines(path_or_io, **options)
1928
1928
  #
1929
1929
  # Alias for CSV.read.
1930
- def readlines(path, **options)
1931
- read(path, **options)
1930
+ def readlines(path_or_io, **options)
1931
+ read(path_or_io, **options)
1932
1932
  end
1933
1933
 
1934
1934
  # :call-seq:
1935
- # CSV.table(source, **options)
1935
+ # CSV.table(path_or_io, **options)
1936
1936
  #
1937
- # Calls CSV.read with +source+, +options+, and certain default options:
1937
+ # Calls CSV.read with +path_or_io+, +options+, and certain default options:
1938
1938
  # - +headers+: +true+
1939
1939
  # - +converters+: +:numeric+
1940
1940
  # - +header_converters+: +:symbol+
@@ -1946,25 +1946,25 @@ class CSV
1946
1946
  # path = 't.csv'
1947
1947
  # File.write(path, string)
1948
1948
  # CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:4>
1949
- def table(path, **options)
1949
+ def table(path_or_io, **options)
1950
1950
  default_options = {
1951
1951
  headers: true,
1952
1952
  converters: :numeric,
1953
1953
  header_converters: :symbol,
1954
1954
  }
1955
1955
  options = default_options.merge(options)
1956
- read(path, **options)
1956
+ read(path_or_io, **options)
1957
1957
  end
1958
1958
 
1959
1959
  ON_WINDOWS = /mingw|mswin/.match?(RUBY_PLATFORM)
1960
1960
  private_constant :ON_WINDOWS
1961
1961
 
1962
1962
  private
1963
- def may_enable_bom_detection_automatically(filename_or_io,
1963
+ def may_enable_bom_detection_automatically(path_or_io,
1964
1964
  mode,
1965
1965
  options,
1966
1966
  file_opts)
1967
- if filename_or_io.is_a?(StringIO)
1967
+ if path_or_io.is_a?(StringIO)
1968
1968
  # Support to StringIO was dropped for Ruby 2.6 and earlier without BOM support:
1969
1969
  # https://github.com/ruby/stringio/pull/47
1970
1970
  return if RUBY_VERSION < "2.7"
@@ -2110,8 +2110,6 @@ class CSV
2110
2110
  strip: strip,
2111
2111
  }
2112
2112
  @parser = nil
2113
- @parser_enumerator = nil
2114
- @eof_error = nil
2115
2113
 
2116
2114
  @writer_options = {
2117
2115
  encoding: @encoding,
@@ -2327,7 +2325,7 @@ class CSV
2327
2325
  attr_reader :encoding
2328
2326
 
2329
2327
  # :call-seq:
2330
- # csv.line_no -> integer
2328
+ # csv.lineno -> integer
2331
2329
  #
2332
2330
  # Returns the count of the rows parsed or generated.
2333
2331
  #
@@ -2430,24 +2428,13 @@ class CSV
2430
2428
  end
2431
2429
 
2432
2430
  def eof?
2433
- return false if @eof_error
2434
- begin
2435
- parser_enumerator.peek
2436
- false
2437
- rescue MalformedCSVError => error
2438
- @eof_error = error
2439
- false
2440
- rescue StopIteration
2441
- true
2442
- end
2431
+ parser.eof?
2443
2432
  end
2444
2433
  alias_method :eof, :eof?
2445
2434
 
2446
2435
  # Rewinds the underlying IO object and resets CSV's lineno() counter.
2447
2436
  def rewind
2448
2437
  @parser = nil
2449
- @parser_enumerator = nil
2450
- @eof_error = nil
2451
2438
  @writer.rewind if @writer
2452
2439
  @io.rewind
2453
2440
  end
@@ -2687,13 +2674,7 @@ class CSV
2687
2674
  # p row
2688
2675
  # end
2689
2676
  def each(&block)
2690
- return to_enum(__method__) unless block_given?
2691
- begin
2692
- while true
2693
- yield(parser_enumerator.next)
2694
- end
2695
- rescue StopIteration
2696
- end
2677
+ parser.parse(&block)
2697
2678
  end
2698
2679
 
2699
2680
  # :call-seq:
@@ -2801,15 +2782,10 @@ class CSV
2801
2782
  # # Raises IOError (not opened for reading)
2802
2783
  # csv.shift
2803
2784
  def shift
2804
- if @eof_error
2805
- eof_error, @eof_error = @eof_error, nil
2806
- raise eof_error
2807
- end
2808
- begin
2809
- parser_enumerator.next
2810
- rescue StopIteration
2811
- nil
2785
+ parser.parse do |row|
2786
+ return row
2812
2787
  end
2788
+ nil
2813
2789
  end
2814
2790
  alias_method :gets, :shift
2815
2791
  alias_method :readline, :shift
@@ -2971,10 +2947,6 @@ class CSV
2971
2947
  fields_converter: parser_fields_converter)
2972
2948
  end
2973
2949
 
2974
- def parser_enumerator
2975
- @parser_enumerator ||= parser.parse
2976
- end
2977
-
2978
2950
  def writer
2979
2951
  @writer ||= Writer.new(@io, writer_options)
2980
2952
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.5
4
+ version: 3.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Edward Gray II
@@ -71,7 +71,7 @@ licenses:
71
71
  - Ruby
72
72
  - BSD-2-Clause
73
73
  metadata:
74
- changelog_uri: https://github.com/ruby/csv/releases/tag/v3.3.5
74
+ changelog_uri: https://github.com/ruby/csv/releases/tag/v3.3.7
75
75
  rdoc_options:
76
76
  - "--main"
77
77
  - README.md
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 3.6.7
91
+ rubygems_version: 4.0.20
92
92
  specification_version: 4
93
93
  summary: CSV Reading and Writing
94
94
  test_files: []