Skip to content

Commit 6106b7b

Browse files
BurdetteLamarnobu
authored andcommitted
[ruby/csv] Added headers to RDoc for CSV.foreach (ruby#142)
* Added headers: to RDoc for CSV.foreach * Correct options remark for CSV.generate * Improve citation for option headers ruby/csv@b01945ec3a
1 parent e4742fe commit 6106b7b

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

lib/csv.rb

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,8 @@ def filter(input=nil, output=nil, **options)
825825
# :call-seq:
826826
# foreach(path, mode='r', **options) {|row| ... ) -> integer or nil
827827
# 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
828830
# foreach(path, mode='r', **options) -> new_enumerator
829831
# foreach(io, mode='r', **options -> new_enumerator
830832
#
@@ -848,7 +850,9 @@ def filter(input=nil, output=nil, **options)
848850
# would read +UTF-32BE+ data from the file
849851
# but transcode it to +UTF-8+ before parsing.
850852
#
851-
# ---
853+
# ====== Without Option +headers+
854+
#
855+
# Without option +headers+, returns each row as an \Array object.
852856
#
853857
# These examples assume prior execution of:
854858
# string = "foo,0\nbar,1\nbaz,2\n"
@@ -882,6 +886,34 @@ def filter(input=nil, output=nil, **options)
882886
# warning: Unsupported encoding foo ignored
883887
# warning: Unsupported encoding bar ignored
884888
#
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+
#
885917
# ---
886918
#
887919
# 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)
911943
#
912944
# * Argument +csv_string+, if given, must be a \String object;
913945
# 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].
916948
#
917949
# ---
918950
#
@@ -1145,15 +1177,15 @@ def open(filename, mode="r", **options)
11451177
# :include: ../doc/argument_io.rdoc
11461178
# - Argument +options+: see {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
11471179
#
1180+
# ====== Without Option +headers+
1181+
#
1182+
# Without option +headers+, returns an \Array of Arrays or an integer.
1183+
#
11481184
# These examples assume prior execution of:
11491185
# string = "foo,0\nbar,1\nbaz,2\n"
11501186
# path = 't.csv'
11511187
# File.write(path, string)
11521188
#
1153-
# ====== Without Option +headers+
1154-
#
1155-
# Without option +headers+, returns an \Array of Arrays or an integer.
1156-
#
11571189
# ---
11581190
#
11591191
# With no block given, returns an \Array of Arrays formed from the source.
@@ -1195,6 +1227,11 @@ def open(filename, mode="r", **options)
11951227
# With {option +headers+}[#class-CSV-label-Option+headers],
11961228
# returns a new CSV::Table object or an integer.
11971229
#
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+
#
11981235
# ---
11991236
#
12001237
# With no block given, returns a CSV::Table object formed from the source.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy