@@ -825,6 +825,8 @@ def filter(input=nil, output=nil, **options)
825
825
# :call-seq:
826
826
# foreach(path, mode='r', **options) {|row| ... ) -> integer or nil
827
827
# foreach(io, mode='r', **options {|row| ... ) -> integer or nil
828
+ # foreach(path, mode='r', headers: ..., **options) {|row| ... ) -> integer or nil
829
+ # foreach(io, mode='r', headers: ..., **options {|row| ... ) -> integer or nil
828
830
# foreach(path, mode='r', **options) -> new_enumerator
829
831
# foreach(io, mode='r', **options -> new_enumerator
830
832
#
@@ -848,7 +850,9 @@ def filter(input=nil, output=nil, **options)
848
850
# would read +UTF-32BE+ data from the file
849
851
# but transcode it to +UTF-8+ before parsing.
850
852
#
851
- # ---
853
+ # ====== Without Option +headers+
854
+ #
855
+ # Without option +headers+, returns each row as an \Array object.
852
856
#
853
857
# These examples assume prior execution of:
854
858
# string = "foo,0\nbar,1\nbaz,2\n"
@@ -882,6 +886,34 @@ def filter(input=nil, output=nil, **options)
882
886
# warning: Unsupported encoding foo ignored
883
887
# warning: Unsupported encoding bar ignored
884
888
#
889
+ # ====== With Option +headers+
890
+ #
891
+ # With {option +headers+}[#class-CSV-label-Option+headers],
892
+ # returns each row as a CSV::Row object.
893
+ #
894
+ # These examples assume prior execution of:
895
+ # string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
896
+ # path = 't.csv'
897
+ # File.write(path, string)
898
+ #
899
+ # Read rows from a file at +path+:
900
+ # CSV.foreach(path, headers: true) {|row| p row } # => 21
901
+ #
902
+ # Output:
903
+ # #<CSV::Row "Name":"foo" "Count":"0">
904
+ # #<CSV::Row "Name":"bar" "Count":"1">
905
+ # #<CSV::Row "Name":"baz" "Count":"2">
906
+ #
907
+ # Read rows from an \IO object:
908
+ # File.open(path) do |file|
909
+ # CSV.foreach(file, headers: true) {|row| p row } # => 21
910
+ # end
911
+ #
912
+ # Output:
913
+ # #<CSV::Row "Name":"foo" "Count":"0">
914
+ # #<CSV::Row "Name":"bar" "Count":"1">
915
+ # #<CSV::Row "Name":"baz" "Count":"2">
916
+ #
885
917
# ---
886
918
#
887
919
# Raises an exception if +path+ is a \String, but not the path to a readable file:
@@ -911,8 +943,8 @@ def foreach(path, mode="r", **options, &block)
911
943
#
912
944
# * Argument +csv_string+, if given, must be a \String object;
913
945
# defaults to a new empty \String.
914
- # * Arguments +options+, if given, should be parsing options.
915
- # See {Options for Parsing }[#class-CSV-label-Options+for+Parsing ].
946
+ # * Arguments +options+, if given, should be generating options.
947
+ # See {Options for Generating }[#class-CSV-label-Options+for+Generating ].
916
948
#
917
949
# ---
918
950
#
@@ -1145,15 +1177,15 @@ def open(filename, mode="r", **options)
1145
1177
# :include: ../doc/argument_io.rdoc
1146
1178
# - Argument +options+: see {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
1147
1179
#
1180
+ # ====== Without Option +headers+
1181
+ #
1182
+ # Without option +headers+, returns an \Array of Arrays or an integer.
1183
+ #
1148
1184
# These examples assume prior execution of:
1149
1185
# string = "foo,0\nbar,1\nbaz,2\n"
1150
1186
# path = 't.csv'
1151
1187
# File.write(path, string)
1152
1188
#
1153
- # ====== Without Option +headers+
1154
- #
1155
- # Without option +headers+, returns an \Array of Arrays or an integer.
1156
- #
1157
1189
# ---
1158
1190
#
1159
1191
# With no block given, returns an \Array of Arrays formed from the source.
@@ -1195,6 +1227,11 @@ def open(filename, mode="r", **options)
1195
1227
# With {option +headers+}[#class-CSV-label-Option+headers],
1196
1228
# returns a new CSV::Table object or an integer.
1197
1229
#
1230
+ # These examples assume prior execution of:
1231
+ # string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
1232
+ # path = 't.csv'
1233
+ # File.write(path, string)
1234
+ #
1198
1235
# ---
1199
1236
#
1200
1237
# With no block given, returns a CSV::Table object formed from the source.
0 commit comments