-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathcsv.rbs
More file actions
3810 lines (3733 loc) · 124 KB
/
csv.rbs
File metadata and controls
3810 lines (3733 loc) · 124 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=lib/csv.rb -->
# ## CSV
#
# ### CSV Data
#
# CSV (comma-separated values) data is a text representation of a table:
# * A *row* *separator* delimits table rows. A common row separator is the
# newline character `"\n"`.
# * A *column* *separator* delimits fields in a row. A common column separator
# is the comma character `","`.
#
#
# This CSV String, with row separator `"\n"` and column separator `","`, has
# three rows and two columns:
# "foo,0\nbar,1\nbaz,2\n"
#
# Despite the name CSV, a CSV representation can use different separators.
#
# For more about tables, see the Wikipedia article "[Table
# (information)](https://en.wikipedia.org/wiki/Table_(information))", especially
# its section "[Simple
# table](https://en.wikipedia.org/wiki/Table_(information)#Simple_table)"
#
# ## Class CSV
#
# Class CSV provides methods for:
# * Parsing CSV data from a String object, a File (via its file path), or an
# IO object.
# * Generating CSV data to a String object.
#
#
# To make CSV available:
# require 'csv'
#
# All examples here assume that this has been done.
#
# ## Keeping It Simple
#
# A CSV object has dozens of instance methods that offer fine-grained control of
# parsing and generating CSV data. For many needs, though, simpler approaches
# will do.
#
# This section summarizes the singleton methods in CSV that allow you to parse
# and generate without explicitly creating CSV objects. For details, follow the
# links.
#
# ### Simple Parsing
#
# Parsing methods commonly return either of:
# * An Array of Arrays of Strings:
# * The outer Array is the entire "table".
# * Each inner Array is a row.
# * Each String is a field.
#
# * A CSV::Table object. For details, see [\CSV with
# Headers](#class-CSV-label-CSV+with+Headers).
#
#
# #### Parsing a String
#
# The input to be parsed can be a string:
# string = "foo,0\nbar,1\nbaz,2\n"
#
# Method CSV.parse returns the entire CSV data:
# CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Method CSV.parse_line returns only the first row:
# CSV.parse_line(string) # => ["foo", "0"]
#
# CSV extends class String with instance method String#parse_csv, which also
# returns only the first row:
# string.parse_csv # => ["foo", "0"]
#
# #### Parsing Via a File Path
#
# The input to be parsed can be in a file:
# string = "foo,0\nbar,1\nbaz,2\n"
# path = 't.csv'
# File.write(path, string)
#
# Method CSV.read returns the entire CSV data:
# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Method CSV.foreach iterates, passing each row to the given block:
# CSV.foreach(path) do |row|
# p row
# end
#
# Output:
# ["foo", "0"]
# ["bar", "1"]
# ["baz", "2"]
#
# Method CSV.table returns the entire CSV data as a CSV::Table object:
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:3>
#
# #### Parsing from an Open IO Stream
#
# The input to be parsed can be in an open IO stream:
#
# Method CSV.read returns the entire CSV data:
# File.open(path) do |file|
# CSV.read(file)
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# As does method CSV.parse:
# File.open(path) do |file|
# CSV.parse(file)
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Method CSV.parse_line returns only the first row:
# File.open(path) do |file|
# CSV.parse_line(file)
# end # => ["foo", "0"]
#
# Method CSV.foreach iterates, passing each row to the given block:
# File.open(path) do |file|
# CSV.foreach(file) do |row|
# p row
# end
# end
#
# Output:
# ["foo", "0"]
# ["bar", "1"]
# ["baz", "2"]
#
# Method CSV.table returns the entire CSV data as a CSV::Table object:
# File.open(path) do |file|
# CSV.table(file)
# end # => #<CSV::Table mode:col_or_row row_count:3>
#
# ### Simple Generating
#
# Method CSV.generate returns a String; this example uses method CSV#<< to
# append the rows that are to be generated:
# output_string = CSV.generate do |csv|
# csv << ['foo', 0]
# csv << ['bar', 1]
# csv << ['baz', 2]
# end
# output_string # => "foo,0\nbar,1\nbaz,2\n"
#
# Method CSV.generate_line returns a String containing the single row
# constructed from an Array:
# CSV.generate_line(['foo', '0']) # => "foo,0\n"
#
# CSV extends class Array with instance method `Array#to_csv`, which forms an
# Array into a String:
# ['foo', '0'].to_csv # => "foo,0\n"
#
# ### "Filtering" CSV
#
# Method CSV.filter provides a Unix-style filter for CSV data. The input data is
# processed to form the output data:
# in_string = "foo,0\nbar,1\nbaz,2\n"
# out_string = ''
# CSV.filter(in_string, out_string) do |row|
# row[0] = row[0].upcase
# row[1] *= 4
# end
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
#
# ## CSV Objects
#
# There are three ways to create a CSV object:
# * Method CSV.new returns a new CSV object.
# * Method CSV.instance returns a new or cached CSV object.
# * Method CSV() also returns a new or cached CSV object.
#
#
# ### Instance Methods
#
# CSV has three groups of instance methods:
# * Its own internally defined instance methods.
# * Methods included by module Enumerable.
# * Methods delegated to class IO. See below.
#
#
# #### Delegated Methods
#
# For convenience, a CSV object will delegate to many methods in class IO. (A
# few have wrapper "guard code" in CSV.) You may call:
# * IO#binmode
# * #binmode?
# * IO#close
# * IO#close_read
# * IO#close_write
# * IO#closed?
# * #eof
# * #eof?
# * IO#external_encoding
# * IO#fcntl
# * IO#fileno
# * #flock
# * IO#flush
# * IO#fsync
# * IO#internal_encoding
# * #ioctl
# * IO#isatty
# * #path
# * IO#pid
# * IO#pos
# * IO#pos=
# * IO#reopen
# * #rewind
# * IO#seek
# * #stat
# * IO#string
# * IO#sync
# * IO#sync=
# * IO#tell
# * #to_i
# * #to_io
# * IO#truncate
# * IO#tty?
#
#
# ### Options
#
# The default values for options are:
# DEFAULT_OPTIONS = {
# # For both parsing and generating.
# col_sep: ",",
# row_sep: :auto,
# quote_char: '"',
# # For parsing.
# field_size_limit: nil,
# converters: nil,
# unconverted_fields: nil,
# headers: false,
# return_headers: false,
# header_converters: nil,
# skip_blanks: false,
# skip_lines: nil,
# liberal_parsing: false,
# nil_value: nil,
# empty_value: "",
# strip: false,
# # For generating.
# write_headers: nil,
# quote_empty: true,
# force_quotes: false,
# write_converters: nil,
# write_nil_value: nil,
# write_empty_value: "",
# }
#
# #### Options for Parsing
#
# Options for parsing, described in detail below, include:
# * `row_sep`: Specifies the row separator; used to delimit rows.
# * `col_sep`: Specifies the column separator; used to delimit fields.
# * `quote_char`: Specifies the quote character; used to quote fields.
# * `field_size_limit`: Specifies the maximum field size + 1 allowed.
# Deprecated since 3.2.3. Use `max_field_size` instead.
# * `max_field_size`: Specifies the maximum field size allowed.
# * `converters`: Specifies the field converters to be used.
# * `unconverted_fields`: Specifies whether unconverted fields are to be
# available.
# * `headers`: Specifies whether data contains headers, or specifies the
# headers themselves.
# * `return_headers`: Specifies whether headers are to be returned.
# * `header_converters`: Specifies the header converters to be used.
# * `skip_blanks`: Specifies whether blanks lines are to be ignored.
# * `skip_lines`: Specifies how comments lines are to be recognized.
# * `strip`: Specifies whether leading and trailing whitespace are to be
# stripped from fields. This must be compatible with `col_sep`; if it is
# not, then an `ArgumentError` exception will be raised.
# * `liberal_parsing`: Specifies whether CSV should attempt to parse
# non-compliant data.
# * `nil_value`: Specifies the object that is to be substituted for each null
# (no-text) field.
# * `empty_value`: Specifies the object that is to be substituted for each
# empty field.
#
#
# ###### Option `row_sep`
#
# Specifies the row separator, a String or the Symbol `:auto` (see below), to be
# used for both parsing and generating.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:row_sep) # => :auto
#
# ---
#
# When `row_sep` is a String, that String becomes the row separator. The String
# will be transcoded into the data's Encoding before use.
#
# Using `"\n"`:
# row_sep = "\n"
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
# ary = CSV.parse(str)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `|` (pipe):
# row_sep = '|'
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0|bar,1|baz,2|"
# ary = CSV.parse(str, row_sep: row_sep)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `--` (two hyphens):
# row_sep = '--'
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0--bar,1--baz,2--"
# ary = CSV.parse(str, row_sep: row_sep)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `''` (empty string):
# row_sep = ''
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0bar,1baz,2"
# ary = CSV.parse(str, row_sep: row_sep)
# ary # => [["foo", "0bar", "1baz", "2"]]
#
# ---
#
# When `row_sep` is the Symbol `:auto` (the default), generating uses `"\n"` as
# the row separator:
# str = CSV.generate do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
#
# Parsing, on the other hand, invokes auto-discovery of the row separator.
#
# Auto-discovery reads ahead in the data looking for the next `\r\n`, `\n`, or
# `\r` sequence. The sequence will be selected even if it occurs in a quoted
# field, assuming that you would have the same line endings there.
#
# Example:
# str = CSV.generate do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
# ary = CSV.parse(str)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# The default `$INPUT_RECORD_SEPARATOR` (`$/`) is used if any of the following
# is true:
# * None of those sequences is found.
# * Data is `ARGF`, `STDIN`, `STDOUT`, or `STDERR`.
# * The stream is only available for output.
#
#
# Obviously, discovery takes a little time. Set manually if speed is important.
# Also note that IO objects should be opened in binary mode on Windows if this
# feature will be used as the line-ending translation can cause problems with
# resetting the document position to where it was before the read ahead.
#
# ###### Option `col_sep`
#
# Specifies the String field separator to be used for both parsing and
# generating. The String will be transcoded into the data's Encoding before use.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
#
# Using the default (comma):
# str = CSV.generate do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
# ary = CSV.parse(str)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `:` (colon):
# col_sep = ':'
# str = CSV.generate(col_sep: col_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo:0\nbar:1\nbaz:2\n"
# ary = CSV.parse(str, col_sep: col_sep)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `::` (two colons):
# col_sep = '::'
# str = CSV.generate(col_sep: col_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo::0\nbar::1\nbaz::2\n"
# ary = CSV.parse(str, col_sep: col_sep)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `''` (empty string):
# col_sep = ''
# str = CSV.generate(col_sep: col_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo0\nbar1\nbaz2\n"
#
# ---
#
# Raises an exception if parsing with the empty String:
# col_sep = ''
# # Raises ArgumentError (:col_sep must be 1 or more characters: "")
# CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
#
# ###### Option `quote_char`
#
# Specifies the character (String of length 1) used used to quote fields in both
# parsing and generating. This String will be transcoded into the data's
# Encoding before use.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:quote_char) # => "\"" (double quote)
#
# This is useful for an application that incorrectly uses `'` (single-quote) to
# quote fields, instead of the correct `"` (double-quote).
#
# Using the default (double quote):
# str = CSV.generate do |csv|
# csv << ['foo', 0]
# csv << ["'bar'", 1]
# csv << ['"baz"', 2]
# end
# str # => "foo,0\n'bar',1\n\"\"\"baz\"\"\",2\n"
# ary = CSV.parse(str)
# ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
#
# Using `'` (single-quote):
# quote_char = "'"
# str = CSV.generate(quote_char: quote_char) do |csv|
# csv << ['foo', 0]
# csv << ["'bar'", 1]
# csv << ['"baz"', 2]
# end
# str # => "foo,0\n'''bar''',1\n\"baz\",2\n"
# ary = CSV.parse(str, quote_char: quote_char)
# ary # => [["foo", "0"], ["'bar'", "1"], ["\"baz\"", "2"]]
#
# ---
#
# Raises an exception if the String length is greater than 1:
# # Raises ArgumentError (:quote_char has to be nil or a single character String)
# CSV.new('', quote_char: 'xx')
#
# Raises an exception if the value is not a String:
# # Raises ArgumentError (:quote_char has to be nil or a single character String)
# CSV.new('', quote_char: :foo)
#
# ###### Option `field_size_limit`
#
# Specifies the Integer field size limit.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:field_size_limit) # => nil
#
# This is a maximum size CSV will read ahead looking for the closing quote for a
# field. (In truth, it reads to the first line ending beyond this size.) If a
# quote cannot be found within the limit CSV will raise a MalformedCSVError,
# assuming the data is faulty. You can use this limit to prevent what are
# effectively DoS attacks on the parser. However, this limit can cause a
# legitimate parse to fail; therefore the default value is `nil` (no limit).
#
# For the examples in this section:
# str = <<~EOT
# "a","b"
# "
# 2345
# ",""
# EOT
# str # => "\"a\",\"b\"\n\"\n2345\n\",\"\"\n"
#
# Using the default `nil`:
# ary = CSV.parse(str)
# ary # => [["a", "b"], ["\n2345\n", ""]]
#
# Using `50`:
# field_size_limit = 50
# ary = CSV.parse(str, field_size_limit: field_size_limit)
# ary # => [["a", "b"], ["\n2345\n", ""]]
#
# ---
#
# Raises an exception if a field is too long:
# big_str = "123456789\n" * 1024
# # Raises CSV::MalformedCSVError (Field size exceeded in line 1.)
# CSV.parse('valid,fields,"' + big_str + '"', field_size_limit: 2048)
#
# ###### Option `converters`
#
# Specifies converters to be used in parsing fields. See [Field
# Converters](#class-CSV-label-Field+Converters)
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:converters) # => nil
#
# The value may be a field converter name (see [Stored
# Converters](#class-CSV-label-Stored+Converters)):
# str = '1,2,3'
# # Without a converter
# array = CSV.parse_line(str)
# array # => ["1", "2", "3"]
# # With built-in converter :integer
# array = CSV.parse_line(str, converters: :integer)
# array # => [1, 2, 3]
#
# The value may be a converter list (see [Converter
# Lists](#class-CSV-label-Converter+Lists)):
# str = '1,3.14159'
# # Without converters
# array = CSV.parse_line(str)
# array # => ["1", "3.14159"]
# # With built-in converters
# array = CSV.parse_line(str, converters: [:integer, :float])
# array # => [1, 3.14159]
#
# The value may be a Proc custom converter: (see [Custom Field
# Converters](#class-CSV-label-Custom+Field+Converters)):
# str = ' foo , bar , baz '
# # Without a converter
# array = CSV.parse_line(str)
# array # => [" foo ", " bar ", " baz "]
# # With a custom converter
# array = CSV.parse_line(str, converters: proc {|field| field.strip })
# array # => ["foo", "bar", "baz"]
#
# See also [Custom Field Converters](#class-CSV-label-Custom+Field+Converters)
#
# ---
#
# Raises an exception if the converter is not a converter name or a Proc:
# str = 'foo,0'
# # Raises NoMethodError (undefined method `arity' for nil:NilClass)
# CSV.parse(str, converters: :foo)
#
# ###### Option `unconverted_fields`
#
# Specifies the boolean that determines whether unconverted field values are to
# be available.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:unconverted_fields) # => nil
#
# The unconverted field values are those found in the source data, prior to any
# conversions performed via option `converters`.
#
# When option `unconverted_fields` is `true`, each returned row (Array or
# CSV::Row) has an added method, `unconverted_fields`, that returns the
# unconverted field values:
# str = <<-EOT
# foo,0
# bar,1
# baz,2
# EOT
# # Without unconverted_fields
# csv = CSV.parse(str, converters: :integer)
# csv # => [["foo", 0], ["bar", 1], ["baz", 2]]
# csv.first.respond_to?(:unconverted_fields) # => false
# # With unconverted_fields
# csv = CSV.parse(str, converters: :integer, unconverted_fields: true)
# csv # => [["foo", 0], ["bar", 1], ["baz", 2]]
# csv.first.respond_to?(:unconverted_fields) # => true
# csv.first.unconverted_fields # => ["foo", "0"]
#
# ###### Option `headers`
#
# Specifies a boolean, Symbol, Array, or String to be used to define column
# headers.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:headers) # => false
#
# ---
#
# Without `headers`:
# str = <<-EOT
# Name,Count
# foo,0
# bar,1
# bax,2
# EOT
# csv = CSV.new(str)
# csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"">
# csv.headers # => nil
# csv.shift # => ["Name", "Count"]
#
# ---
#
# If set to `true` or the Symbol `:first_row`, the first row of the data is
# treated as a row of headers:
# str = <<-EOT
# Name,Count
# foo,0
# bar,1
# bax,2
# EOT
# csv = CSV.new(str, headers: true)
# csv # => #<CSV io_type:StringIO encoding:UTF-8 lineno:2 col_sep:"," row_sep:"\n" quote_char:"\"" headers:["Name", "Count"]>
# csv.headers # => ["Name", "Count"]
# csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
#
# ---
#
# If set to an Array, the Array elements are treated as headers:
# str = <<-EOT
# foo,0
# bar,1
# bax,2
# EOT
# csv = CSV.new(str, headers: ['Name', 'Count'])
# csv
# csv.headers # => ["Name", "Count"]
# csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
#
# ---
#
# If set to a String `str`, method `CSV::parse_line(str, options)` is called
# with the current `options`, and the returned Array is treated as headers:
# str = <<-EOT
# foo,0
# bar,1
# bax,2
# EOT
# csv = CSV.new(str, headers: 'Name,Count')
# csv
# csv.headers # => ["Name", "Count"]
# csv.shift # => #<CSV::Row "Name":"bar" "Count":"1">
#
# ###### Option `return_headers`
#
# Specifies the boolean that determines whether method #shift returns or ignores
# the header row.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:return_headers) # => false
#
# Examples:
# str = <<-EOT
# Name,Count
# foo,0
# bar,1
# bax,2
# EOT
# # Without return_headers first row is str.
# csv = CSV.new(str, headers: true)
# csv.shift # => #<CSV::Row "Name":"foo" "Count":"0">
# # With return_headers first row is headers.
# csv = CSV.new(str, headers: true, return_headers: true)
# csv.shift # => #<CSV::Row "Name":"Name" "Count":"Count">
#
# ###### Option `header_converters`
#
# Specifies converters to be used in parsing headers. See [Header
# Converters](#class-CSV-label-Header+Converters)
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:header_converters) # => nil
#
# Identical in functionality to option
# [converters](#class-CSV-label-Option+converters) except that:
# * The converters apply only to the header row.
# * The built-in header converters are `:downcase` and `:symbol`.
#
#
# This section assumes prior execution of:
# str = <<-EOT
# Name,Value
# foo,0
# bar,1
# baz,2
# EOT
# # With no header converter
# table = CSV.parse(str, headers: true)
# table.headers # => ["Name", "Value"]
#
# The value may be a header converter name (see [Stored
# Converters](#class-CSV-label-Stored+Converters)):
# table = CSV.parse(str, headers: true, header_converters: :downcase)
# table.headers # => ["name", "value"]
#
# The value may be a converter list (see [Converter
# Lists](#class-CSV-label-Converter+Lists)):
# header_converters = [:downcase, :symbol]
# table = CSV.parse(str, headers: true, header_converters: header_converters)
# table.headers # => [:name, :value]
#
# The value may be a Proc custom converter (see [Custom Header
# Converters](#class-CSV-label-Custom+Header+Converters)):
# upcase_converter = proc {|field| field.upcase }
# table = CSV.parse(str, headers: true, header_converters: upcase_converter)
# table.headers # => ["NAME", "VALUE"]
#
# See also [Custom Header Converters](#class-CSV-label-Custom+Header+Converters)
#
# ###### Option `skip_blanks`
#
# Specifies a boolean that determines whether blank lines in the input will be
# ignored; a line that contains a column separator is not considered to be
# blank.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:skip_blanks) # => false
#
# See also option [skiplines](#class-CSV-label-Option+skip_lines).
#
# For examples in this section:
# str = <<-EOT
# foo,0
#
# bar,1
# baz,2
#
# ,
# EOT
#
# Using the default, `false`:
# ary = CSV.parse(str)
# ary # => [["foo", "0"], [], ["bar", "1"], ["baz", "2"], [], [nil, nil]]
#
# Using `true`:
# ary = CSV.parse(str, skip_blanks: true)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"], [nil, nil]]
#
# Using a truthy value:
# ary = CSV.parse(str, skip_blanks: :foo)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"], [nil, nil]]
#
# ###### Option `skip_lines`
#
# Specifies an object to use in identifying comment lines in the input that are
# to be ignored:
# * If a Regexp, ignores lines that match it.
# * If a String, converts it to a Regexp, ignores lines that match it.
# * If `nil`, no lines are considered to be comments.
#
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:skip_lines) # => nil
#
# For examples in this section:
# str = <<-EOT
# # Comment
# foo,0
# bar,1
# baz,2
# # Another comment
# EOT
# str # => "# Comment\nfoo,0\nbar,1\nbaz,2\n# Another comment\n"
#
# Using the default, `nil`:
# ary = CSV.parse(str)
# ary # => [["# Comment"], ["foo", "0"], ["bar", "1"], ["baz", "2"], ["# Another comment"]]
#
# Using a Regexp:
# ary = CSV.parse(str, skip_lines: /^#/)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using a String:
# ary = CSV.parse(str, skip_lines: '#')
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# ---
#
# Raises an exception if given an object that is not a Regexp, a String, or
# `nil`:
# # Raises ArgumentError (:skip_lines has to respond to #match: 0)
# CSV.parse(str, skip_lines: 0)
#
# ###### Option `strip`
#
# Specifies the boolean value that determines whether whitespace is stripped
# from each input field.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:strip) # => false
#
# With default value `false`:
# ary = CSV.parse_line(' a , b ')
# ary # => [" a ", " b "]
#
# With value `true`:
# ary = CSV.parse_line(' a , b ', strip: true)
# ary # => ["a", "b"]
#
# ###### Option `liberal_parsing`
#
# Specifies the boolean or hash value that determines whether CSV will attempt
# to parse input not conformant with RFC 4180, such as double quotes in unquoted
# fields.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:liberal_parsing) # => false
#
# For the next two examples:
# str = 'is,this "three, or four",fields'
#
# Without `liberal_parsing`:
# # Raises CSV::MalformedCSVError (Illegal quoting in str 1.)
# CSV.parse_line(str)
#
# With `liberal_parsing`:
# ary = CSV.parse_line(str, liberal_parsing: true)
# ary # => ["is", "this \"three", " or four\"", "fields"]
#
# Use the `backslash_quote` sub-option to parse values that use a backslash to
# escape a double-quote character. This causes the parser to treat `\"` as if
# it were `""`.
#
# For the next two examples:
# str = 'Show,"Harry \"Handcuff\" Houdini, the one and only","Tampa Theater"'
#
# With `liberal_parsing`, but without the `backslash_quote` sub-option:
# # Incorrect interpretation of backslash; incorrectly interprets the quoted comma as a field separator.
# ary = CSV.parse_line(str, liberal_parsing: true)
# ary # => ["Show", "\"Harry \\\"Handcuff\\\" Houdini", " the one and only\"", "Tampa Theater"]
# puts ary[1] # => "Harry \"Handcuff\" Houdini
#
# With `liberal_parsing` and its `backslash_quote` sub-option:
# ary = CSV.parse_line(str, liberal_parsing: { backslash_quote: true })
# ary # => ["Show", "Harry \"Handcuff\" Houdini, the one and only", "Tampa Theater"]
# puts ary[1] # => Harry "Handcuff" Houdini, the one and only
#
# ###### Option `nil_value`
#
# Specifies the object that is to be substituted for each null (no-text) field.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:nil_value) # => nil
#
# With the default, `nil`:
# CSV.parse_line('a,,b,,c') # => ["a", nil, "b", nil, "c"]
#
# With a different object:
# CSV.parse_line('a,,b,,c', nil_value: 0) # => ["a", 0, "b", 0, "c"]
#
# ###### Option `empty_value`
#
# Specifies the object that is to be substituted for each field that has an
# empty String.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:empty_value) # => "" (empty string)
#
# With the default, `""`:
# CSV.parse_line('a,"",b,"",c') # => ["a", "", "b", "", "c"]
#
# With a different object:
# CSV.parse_line('a,"",b,"",c', empty_value: 'x') # => ["a", "x", "b", "x", "c"]
#
# #### Options for Generating
#
# Options for generating, described in detail below, include:
# * `row_sep`: Specifies the row separator; used to delimit rows.
# * `col_sep`: Specifies the column separator; used to delimit fields.
# * `quote_char`: Specifies the quote character; used to quote fields.
# * `write_headers`: Specifies whether headers are to be written.
# * `force_quotes`: Specifies whether each output field is to be quoted.
# * `quote_empty`: Specifies whether each empty output field is to be quoted.
# * `write_converters`: Specifies the field converters to be used in writing.
# * `write_nil_value`: Specifies the object that is to be substituted for each
# `nil`-valued field.
# * `write_empty_value`: Specifies the object that is to be substituted for
# each empty field.
#
#
# ###### Option `row_sep`
#
# Specifies the row separator, a String or the Symbol `:auto` (see below), to be
# used for both parsing and generating.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:row_sep) # => :auto
#
# ---
#
# When `row_sep` is a String, that String becomes the row separator. The String
# will be transcoded into the data's Encoding before use.
#
# Using `"\n"`:
# row_sep = "\n"
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
# ary = CSV.parse(str)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `|` (pipe):
# row_sep = '|'
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0|bar,1|baz,2|"
# ary = CSV.parse(str, row_sep: row_sep)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `--` (two hyphens):
# row_sep = '--'
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0--bar,1--baz,2--"
# ary = CSV.parse(str, row_sep: row_sep)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# Using `''` (empty string):
# row_sep = ''
# str = CSV.generate(row_sep: row_sep) do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0bar,1baz,2"
# ary = CSV.parse(str, row_sep: row_sep)
# ary # => [["foo", "0bar", "1baz", "2"]]
#
# ---
#
# When `row_sep` is the Symbol `:auto` (the default), generating uses `"\n"` as
# the row separator:
# str = CSV.generate do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
#
# Parsing, on the other hand, invokes auto-discovery of the row separator.
#
# Auto-discovery reads ahead in the data looking for the next `\r\n`, `\n`, or
# `\r` sequence. The sequence will be selected even if it occurs in a quoted
# field, assuming that you would have the same line endings there.
#
# Example:
# str = CSV.generate do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
# ary = CSV.parse(str)
# ary # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
#
# The default `$INPUT_RECORD_SEPARATOR` (`$/`) is used if any of the following
# is true:
# * None of those sequences is found.
# * Data is `ARGF`, `STDIN`, `STDOUT`, or `STDERR`.
# * The stream is only available for output.
#
#
# Obviously, discovery takes a little time. Set manually if speed is important.
# Also note that IO objects should be opened in binary mode on Windows if this
# feature will be used as the line-ending translation can cause problems with
# resetting the document position to where it was before the read ahead.
#
# ###### Option `col_sep`
#
# Specifies the String field separator to be used for both parsing and
# generating. The String will be transcoded into the data's Encoding before use.
#
# Default value:
# CSV::DEFAULT_OPTIONS.fetch(:col_sep) # => "," (comma)
#
# Using the default (comma):
# str = CSV.generate do |csv|
# csv << [:foo, 0]
# csv << [:bar, 1]
# csv << [:baz, 2]
# end
# str # => "foo,0\nbar,1\nbaz,2\n"
# ary = CSV.parse(str)