8000 [ruby/csv] Revised introductory RDoc (#151) · nobu/ruby@a0bee2b · GitHub
[go: up one dir, main page]

Skip to content

Commit a0bee2b

Browse files
BurdetteLamarnobu
authored andcommitted
[ruby/csv] Revised introductory RDoc (ruby#151)
ruby/csv@60f6f1c28f
1 parent da83401 commit a0bee2b

File tree

1 file changed

+147
-68
lines changed

1 file changed

+147
-68
lines changed

lib/csv.rb

Lines changed: 147 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -103,85 +103,164 @@
103103

104104
using CSV::MatchP if CSV.const_defined?(:MatchP)
105105

106-
# This class provides a complete interface to CSV files and data. It offers
107-
# tools to enable you to read and write to and from Strings or IO objects, as
108-
# needed.
106+
# == \CSV
107+
# \CSV (comma-separated variables) data is a text representation of a table:
108+
# - A _row_ _separator_ delimits table rows.
109+
# A common row separator is the newline character <tt>"\n"</tt>.
110+
# - A _column_ _separator_ delimits fields in a row.
111+
# A common column separator is the comma character <tt>","</tt>.
109112
#
110-
# All examples here assume prior execution of:
113+
# This \CSV \String, with row separator <tt>"\n"</tt>
114+
# and column separator <tt>","</tt>,
115+
# has three rows and two columns:
116+
# "foo,0\nbar,1\nbaz,2\n"
117+
#
118+
# Despite the name \CSV, a \CSV representation can use different separators.
119+
#
120+
# == \Class \CSV
121+
#
122+
# Class \CSV provides methods for:
123+
# - Parsing \CSV data from a \String object, a \File (via its file path), or an \IO object.
124+
# - Generating \CSV data to a \String object.
125+
#
126+
# To make \CSV available:
111127
# require 'csv'
112128
#
113-
# The most generic interface of the library is:
129+
# All examples here assume that this has been done.
114130
#
115-
# csv = CSV.new(io, **options)
131+
# == Keeping It Simple
116132
#
117-
# # Reading: IO object should be open for read
118-
# csv.read # => array of rows
119-
# # or
120-
# csv.each do |row|
121-
# # ...
122-
# end
123-
# # or
124-
# row = csv.shift
133+
# A \CSV object has dozens of instance methods that offer fine-grained control
134+
# of parsing and generating \CSV data.
135+
# For many needs, though, simpler approaches will do.
125136
#
126-
# # Writing: IO object should be open for write
127-
# csv << row
137+
# This section summarizes the singleton methods in \CSV
138+
# that allow you to parse and generate without explicitly
139+
# creating \CSV objects.
140+
# For details, follow the links.
128141
#
129-
# There are several specialized class methods for one-statement reading or writing,
130-
# described in the Specialized Methods section.
142+
# === Simple Parsing
131143
#
132-
# If a String is passed into ::new, it is internally wrapped into a StringIO object.
144+
# Parsing methods commonly return either of:
145+
# - An \Array of Arrays of Strings:
146+
# - The outer \Array is the entire "table".
147+
# - Each inner \Array is a row.
148+
# - Each \String is a field.
149+
# - A CSV::Table object. For details, see
150+
# {\CSV with Headers}[#class-CSV-label-CSV+with+Headers].
133151
#
134-
# +options+ can be used for specifying the particular CSV flavor (column
135-
# separators, row separators, value quoting and so on), and for data conversion,
136-
# see Data Conversion section for the description of the latter.
152+
# ==== Parsing a \String
137153
#
138-
# == Specialized Methods
154+
# The input to be parsed can be a string:
155+
# string = "foo,0\nbar,1\nbaz,2\n"
139156
#
140-
# === Reading
157+
# \Method CSV.parse returns the entire \CSV data:
158+
# CSV.parse(string) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
141159
#
142-
# # From a file: all at once
143-
# arr_of_rows = CSV.read("path/to/file.csv", **options)
144-
# # iterator-style:
145-
# CSV.foreach("path/to/file.csv", **options) do |row|
146-
# # ...
147-
# end
160+
# \Method CSV.parse_line returns only the first row:
161+
# CSV.parse_line(string) # => ["foo", "0"]
148162
#
149-
# # From a string
150-
# arr_of_rows = CSV.parse("CSV,data,String", **options)
151-
# # or
152-
# CSV.parse("CSV,data,String", **options) do |row|
153-
# # ...
154-
# end
163+
# \CSV extends class \String with instance method String#parse_csv,
164+
# which also returns only the first row:
165+
# string.parse_csv # => ["foo", "0"]
155166
#
156-
# === Writing
167+
# ==== Parsing Via a \File Path
157168
#
158-
# # To a file
159-
# CSV.open("path/to/file.csv", "wb") do |csv|
160-
# csv << ["row", "of", "CSV", "data"]
161-
# csv << ["another", "row"]
162-
# # ...
163-
# end
169+
# The input to be parsed can be in a file:
170+
# string = "foo,0\nbar,1\nbaz,2\n"
171+
# path = 't.csv'
172+
# File.write(path, string)
173+
#
174+
# \Method CSV.read returns the entire \CSV data:
175+
# CSV.read(path) # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
176+
#
177+
# \Method CSV.foreach iterates, passing each row to the given block:
178+
# CSV.foreach(path) do |row|
179+
# p row
180+
# end
181+
# Output:
182+
# ["foo", "0"]
183+
# ["bar", "1"]
184+
# ["baz", "2"]
185+
#
186+
# \Method CSV.table returns the entire \CSV data as a CSV::Table object:
187+
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:3>
164188
#
165-
# # To a String
166-
# csv_string = CSV.generate do |csv|
167-
# csv << ["row", "of", "CSV", "data"]
168-
# csv << ["another", "row"]
169-
# # ...
189+
# ==== Parsing from an Open \IO Stream
190+
#
191+
# The input to be parsed can be in an open \IO stream:
192+
#
193+
# \Method CSV.read returns the entire \CSV data:
194+
# File.open(path) do |file|
195+
# CSV.read(file)
196+
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
197+
#
198+
# As does method CSV.parse:
199+
# File.open(path) do |file|
200+
# CSV.parse(file)
201+
# end # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
202+
#
203+
# \Method CSV.parse_line returns only the first row:
204+
# File.open(path) do |file|
205+
# CSV.parse_line(file)
206+
# end # => ["foo", "0"]
207+
#
208+
# \Method CSV.foreach iterates, passing each row to the given block:
209+
# File.open(path) do |file|
210+
# CSV.foreach(file) do |row|
211+
# p row
212+
# end
170213
# end
214+
# Output:
215+
# ["foo", "0"]
216+
# ["bar", "1"]
217+
# ["baz", "2"]
218+
#
219+
# \Method CSV.table returns the entire \CSV data as a CSV::Table object:
220+
# File.open(path) do |file|
221+
# CSV.table(file)
222+
# end # => #<CSV::Table mode:col_or_row row_count:3>
223+
#
224+
# === Simple Generating
225+
#
226+
# \Method CSV.generate returns a \String;
227+
# this example uses method CSV#<< to append the rows
228+
# that are to be generated:
229+
# output_string = CSV.generate do |csv|
230+
# csv << ['foo', 0]
231+
# csv << ['bar', 1]
232+
# csv << ['baz', 2]
233+
# end
234+
# output_string # => "foo,0\nbar,1\nbaz,2\n"
235+
#
236+
# \Method CSV.generate_line returns a \String containing the single row
237+
# constructed from an \Array:
238+
# CSV.generate_line(['foo', '0']) # => "foo,0\n"
239+
#
240+
# \CSV extends class \Array with instance method <tt>Array#to_csv</tt>,
241+
# which forms an \Array into a \String:
242+
# ['foo', '0'].to_csv # => "foo,0\n"
171243
#
172-
# === Shortcuts
244+
# === "Filtering" \CSV
245+
#
246+
# \Method CSV.filter provides a Unix-style filter for \CSV data.
247+
# The input data is processed to form the output data:
248+
# in_string = "foo,0\nbar,1\nbaz,2\n"
249+
# out_string = ''
250+
# CSV.filter(in_string, out_string) do |row|
251+
# row[0] = row[0].upcase
252+
# row[1] *= 4
253+
# end
254+
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
173255
#
174-
# # Core extensions for converting one line
175-
# csv_string = ["CSV", "data"].to_csv # to CSV
176-
# csv_array = "CSV,String".parse_csv # from CSV
256+
# == \CSV Objects
177257
#
178-
# # CSV() method
179-
# CSV { |csv_out| csv_out << %w{my data here} } # to $stdout
180-
# CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String
181-
# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr
182-
# CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin
258+
# There are three ways to create a \CSV object:
259+
# - \Method CSV.new returns a new \CSV object.
260+
# - \Method CSV.instance returns a new or cached \CSV object.
261+
# - \Method \CSV() also returns a new or cached \CSV object.
183262
#
184-
# == Delegated Methods
263+
# === Delegated Methods
185264
#
186265
# For convenience, a CSV object will delegate to many methods in class IO.
187266
# (A few have wrapper "guard code" in \CSV.) You may call:
@@ -219,7 +298,7 @@
219298
# * IO#truncate
220299
# * IO#tty?
221300
#
222-
# == Options
301+
# === Options
223302
#
224303
# The default values for options are:
225304
# DEFAULT_OPTIONS = {
@@ -249,7 +328,7 @@
249328
# strip: false,
250329
# }
251330
#
252-
# === Options for Parsing
331+
# ==== Options for Parsing
253332
#
254333
# :include: ../doc/options/common/col_sep.rdoc
255334
#
@@ -281,7 +360,7 @@
281360
#
282361
# :include: ../doc/options/parsing/empty_value.rdoc
283362
#
284-
# === Options for Generating
363+
# ==== Options for Generating
285364
#
286365
# :include: ../doc/options/common/col_sep.rdoc
287366
#
@@ -301,7 +380,7 @@
301380
#
302381
# :include: ../doc/options/generating/write_empty_value.rdoc
303382
#
304-
# == CSV with headers
383+
# === \CSV with Headers
305384
#
306385
# CSV allows to specify column names of CSV file, whether they are in data, or
307386
# provided separately. If headers are specified, reading methods return an instance
@@ -323,7 +402,7 @@
323402
# data = CSV.parse('Bob,Engineering,1000', headers: %i[name department salary])
324403
# data.first #=> #<CSV::Row name:"Bob" department:"Engineering" salary:"1000">
325404
#
326-
# == \CSV \Converters
405+
# === \CSV \Converters
327406
#
328407
# By default, each field parsed by \CSV is formed into a \String.
329408
# You can use a _converter_ to convert certain fields into other Ruby objects.
@@ -340,7 +419,7 @@
340419
# All \converters try to transcode fields to UTF-8 before converting.
341420
# The conversion will fail if the data cannot be transcoded, leaving the field unchanged.
342421
#
343-
# === Field \Converters
422+
# ==== Field \Converters
344423
#
345424
# There are three ways to use field \converters;
346425
# these examples use built-in field converter +:integer+,
@@ -431,7 +510,7 @@
431510
#
432511
# See {Custom Converters}[#class-CSV-label-Custom+Converters].
433512
#
434-
# === Header \Converters
513+
# ==== Header \Converters
435514
#
436515
# Header converters operate only on headers (and not on other rows).
437516
#
@@ -496,7 +575,7 @@
496575
#
497576
# See {Custom Converters}[#class-CSV-label-Custom+Converters].
498577
#
499-
# === Custom \Converters
578+
# ==== Custom \Converters
500579
#
501580
# You can define custom \converters.
502581
#
@@ -523,7 +602,7 @@
523602
# If the \converter does not need +field_info+, it can be omitted:
524603
# converter = proc {|field| ... }
525604
#
526-
# == CSV and Character Encodings (M17n or Multilingualization)
605+
# === CSV and Character Encodings (M17n or Multilingualization)
527606
#
528607
# This new CSV parser is m17n savvy. The parser works in the Encoding of the IO
529608
# or String object being read from or written to. Your data is never transcoded
@@ -1423,7 +1502,7 @@ def readlines(path, **options)
14231502
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
14241503
# path = 't.csv'
14251504
# File.write(path, string)
1426-
# CSV.table(path # => #<CSV::Table mode:col_or_row row_count:4>
1505+
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:4>
14271506
def table(path, **options)
14281507
default_options = {
14291508
headers: true,

0 commit comments

Comments
 (0)
0