@@ -2080,7 +2080,7 @@ def rewind
2080
2080
### End Delegation ###
2081
2081
2082
2082
# :call-seq:
2083
- # csv. << row
2083
+ # csv << row -> self
2084
2084
#
2085
2085
# Appends a row to +self+.
2086
2086
#
@@ -2120,7 +2120,7 @@ def rewind
2120
2120
# csv << :foo
2121
2121
# end
2122
2122
#
2123
- # Raises an exception if the output stream is not open for writing:
2123
+ # Raises an exception if the output stream is not opened for writing:
2124
2124
# path = 't.csv'
2125
2125
# File.write(path, '')
2126
2126
# File.open(path) do |file|
@@ -2272,25 +2272,57 @@ def header_convert(name = nil, &converter)
2272
2272
2273
2273
include Enumerable
2274
2274
2275
+ # :call-seq:
2276
+ # csv.each -> enumerator
2277
+ # csv.each {|row| ...}
2275
2278
#
2276
- # Yields each row of the data source in turn.
2279
+ # Calls the block with each successive row.
2280
+ # The data source must be opened for reading.
2277
2281
#
2278
- # Support for Enumerable.
2282
+ # Without headers:
2283
+ # string = "foo,0\nbar,1\nbaz,2\n"
2284
+ # csv = CSV.new(string)
2285
+ # csv.each do |row|
2286
+ # p row
2287
+ # end
2288
+ # Output:
2289
+ # ["foo", "0"]
2290
+ # ["bar", "1"]
2291
+ # ["baz", "2"]
2279
2292
#
2280
- # The data source must be open for reading.
2293
+ # With headers:
2294
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2295
+ # csv = CSV.new(string, headers: true)
2296
+ # csv.each do |row|
2297
+ # p row
2298
+ # end
2299
+ # Output:
2300
+ # <CSV::Row "Name":"foo" "Value":"0">
2301
+ # <CSV::Row "Name":"bar" "Value":"1">
2302
+ # <CSV::Row "Name":"baz" "Value":"2">
2303
+ #
2304
+ # ---
2281
2305
#
2306
+ # Raises an exception if the source is not opened for reading:
2307
+ # string = "foo,0\nbar,1\nbaz,2\n"
2308
+ # csv = CSV.new(string)
2309
+ # csv.close
2310
+ # # Raises IOError (not opened for reading)
2311
+ # csv.each do |row|
2312
+ # p row
2313
+ # end
2282
2314
def each ( &block )
2283
2315
parser_enumerator . each ( &block )
2284
2316
end
2285
2317
2286
2318
# :call-seq:
2287
- # read
2319
+ # csv.read -> array or csv_table
2288
2320
#
2289
2321
# Forms the remaining rows from +self+ into:
2290
2322
# - A CSV::Table object, if headers are in use.
2291
- # - An Array of Arrays, otherwise.
2323
+ # - An \ Array of Arrays, otherwise.
2292
2324
#
2293
- # The data source must be open for reading.
2325
+ # The data source must be opened for reading.
2294
2326
#
2295
2327
# Without headers:
2296
2328
# string = "foo,0\nbar,1\nbaz,2\n"
@@ -2305,6 +2337,15 @@ def each(&block)
2305
2337
# File.write(path, string)
2306
2338
# csv = CSV.open(path, headers: true)
2307
2339
# csv.read # => #<CSV::Table mode:col_or_row row_count:4>
2340
+ #
2341
+ # ---
2342
+ #
2343
+ # Raises an exception if the source is not opened for reading:
2344
+ # string = "foo,0\nbar,1\nbaz,2\n"
2345
+ # csv = CSV.new(string)
2346
+ # csv.close
2347
+ # # Raises IOError (not opened for reading)
2348
+ # csv.read
2308
2349
def read
2309
2350
rows = to_a
2310
2351
if parser . use_headers?
@@ -2315,18 +2356,69 @@ def read
2315
2356
end
2316
2357
alias_method :readlines , :read
2317
2358
2318
- # Returns +true+ if the next row read will be a header row.
2359
+ # :call-seq:
2360
+ # csv.header_row? -> true or false
2361
+ #
2362
+ # Returns +true+ if the next row to be read is a header row\;
2363
+ # +false+ otherwise.
2364
+ #
2365
+ # Without headers:
2366
+ # string = "foo,0\nbar,1\nbaz,2\n"
2367
+ # csv = CSV.new(string)
2368
+ # csv.header_row? # => false
2369
+ #
2370
+ # With headers:
2371
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2372
+ # csv = CSV.new(string, headers: true)
2373
+ # csv.header_row? # => true
2374
+ # csv.shift # => #<CSV::Row "Name":"foo" "Value":"0">
2375
+ # csv.header_row? # => false
2376
+ #
2377
+ # ---
2378
+ #
2379
+ # Raises an exception if the source is not opened for reading:
2380
+ # string = "foo,0\nbar,1\nbaz,2\n"
2381
+ # csv = CSV.new(string)
2382
+ # csv.close
2383
+ # # Raises IOError (not opened for reading)
2384
+ # csv.header_row?
2319
2385
def header_row?
2320
2386
parser . header_row?
2321
2387
end
2322
2388
2389
+ # :call-seq:
2390
+ # csv.shift -> array, csv_row, or nil
2323
2391
#
2324
- # The primary read method for wrapped Strings and IOs, a single row is pulled
2325
- # from the data source, parsed and returned as an Array of fields (if header
2326
- # rows are not used) or a CSV::Row (when header rows are used) .
2392
+ # Returns the next row of data as:
2393
+ # - An \ Array if no headers are used.
2394
+ # - A CSV::Row object if headers are used.
2327
2395
#
2328
- # The data source must be open for reading.
2396
+ # The data source must be opened for reading.
2329
2397
#
2398
+ # Without headers:
2399
+ # string = "foo,0\nbar,1\nbaz,2\n"
2400
+ # csv = CSV.new(string)
2401
+ # csv.shift # => ["foo", "0"]
2402
+ # csv.shift # => ["bar", "1"]
2403
+ # csv.shift # => ["baz", "2"]
2404
+ # csv.shift # => nil
2405
+ #
2406
+ # With headers:
2407
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2408
+ # csv = CSV.new(string, headers: true)
2409
+ # csv.shift # => #<CSV::Row "Name":"foo" "Value":"0">
2410
+ # csv.shift # => #<CSV::Row "Name":"bar" "Value":"1">
2411
+ # csv.shift # => #<CSV::Row "Name":"baz" "Value":"2">
2412
+ # csv.shift # => nil
2413
+ #
2414
+ # ---
2415
+ #
2416
+ # Raises an exception if the source is not opened for reading:
2417
+ # string = "foo,0\nbar,1\nbaz,2\n"
2418
+ # csv = CSV.new(string)
2419
+ # csv.close
2420
+ # # Raises IOError (not opened for reading)
2421
+ # csv.shift
2330
2422
def shift
2331
2423
if @eof_error
2332
2424
eof_error , @eof_error = @eof_error , nil
@@ -2341,10 +2433,14 @@ def shift
2341
2433
alias_method :gets , :shift
2342
2434
alias_method :readline , :shift
2343
2435
2436
+ # :call-seq:
2437
+ # csv.inspect -> string
2344
2438
#
2345
- # Returns a simplified description of the key CSV attributes in an
2346
- # ASCII compatible String.
2347
- #
2439
+ # Returns a \String showing certain properties of +self+:
2440
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2441
+ # csv = CSV.new(string, headers: true)
2442
+ # s = csv.inspect
2443
+ # s # => "#<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:\",\" row_sep:\"\\n\" quote_char:\"\\\"\" headers:true>"
2348
2444
def inspect
2349
2445
str = [ "#<" , self . class . to_s , " io_type:" ]
2350
2446
# show type of wrapped IO
0 commit comments