@@ -2080,7 +2080,7 @@ def rewind
20802080 ### End Delegation ###
20812081
20822082 # :call-seq:
2083- # csv. << row
2083+ # csv << row -> self
20842084 #
20852085 # Appends a row to +self+.
20862086 #
@@ -2120,7 +2120,7 @@ def rewind
21202120 # csv << :foo
21212121 # end
21222122 #
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:
21242124 # path = 't.csv'
21252125 # File.write(path, '')
21262126 # File.open(path) do |file|
@@ -2272,25 +2272,57 @@ def header_convert(name = nil, &converter)
22722272
22732273 include Enumerable
22742274
2275+ # :call-seq:
2276+ # csv.each -> enumerator
2277+ # csv.each {|row| ...}
22752278 #
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.
22772281 #
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"]
22792292 #
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+ # ---
22812305 #
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
22822314 def each ( &block )
22832315 parser_enumerator . each ( &block )
22842316 end
22852317
22862318 # :call-seq:
2287- # read
2319+ # csv.read -> array or csv_table
22882320 #
22892321 # Forms the remaining rows from +self+ into:
22902322 # - A CSV::Table object, if headers are in use.
2291- # - An Array of Arrays, otherwise.
2323+ # - An \ Array of Arrays, otherwise.
22922324 #
2293- # The data source must be open for reading.
2325+ # The data source must be opened for reading.
22942326 #
22952327 # Without headers:
22962328 # string = "foo,0\nbar,1\nbaz,2\n"
@@ -2305,6 +2337,15 @@ def each(&block)
23052337 # File.write(path, string)
23062338 # csv = CSV.open(path, headers: true)
23072339 # 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
23082349 def read
23092350 rows = to_a
23102351 if parser . use_headers?
@@ -2315,18 +2356,69 @@ def read
23152356 end
23162357 alias_method :readlines , :read
23172358
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?
23192385 def header_row?
23202386 parser . header_row?
23212387 end
23222388
2389+ # :call-seq:
2390+ # csv.shift -> array, csv_row, or nil
23232391 #
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.
23272395 #
2328- # The data source must be open for reading.
2396+ # The data source must be opened for reading.
23292397 #
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
23302422 def shift
23312423 if @eof_error
23322424 eof_error , @eof_error = @eof_error , nil
@@ -2341,10 +2433,14 @@ def shift
23412433 alias_method :gets , :shift
23422434 alias_method :readline , :shift
23432435
2436+ # :call-seq:
2437+ # csv.inspect -> string
23442438 #
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>"
23482444 def inspect
23492445 str = [ "#<" , self . class . to_s , " io_type:" ]
23502446 # show type of wrapped IO
0 commit comments