rbs 2.8.0 → 2.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/stdlib/csv/0/csv.rbs CHANGED
@@ -1,6 +1,18 @@
1
1
  # <!-- rdoc-file=lib/csv.rb -->
2
2
  # ## CSV
3
- # CSV (comma-separated variables) data is a text representation of a table:
3
+ #
4
+ # ### In a Hurry?
5
+ #
6
+ # If you are familiar with CSV data and have a particular task in mind, you may
7
+ # want to go directly to the:
8
+ # * [Recipes for CSV](doc/csv/recipes/recipes_rdoc.html).
9
+ #
10
+ #
11
+ # Otherwise, read on here, about the API: classes, methods, and constants.
12
+ #
13
+ # ### CSV Data
14
+ #
15
+ # CSV (comma-separated values) data is a text representation of a table:
4
16
  # * A *row* *separator* delimits table rows. A common row separator is the
5
17
  # newline character `"\n"`.
6
18
  # * A *column* *separator* delimits fields in a row. A common column separator
@@ -249,7 +261,9 @@
249
261
  # * `row_sep`: Specifies the row separator; used to delimit rows.
250
262
  # * `col_sep`: Specifies the column separator; used to delimit fields.
251
263
  # * `quote_char`: Specifies the quote character; used to quote fields.
252
- # * `field_size_limit`: Specifies the maximum field size allowed.
264
+ # * `field_size_limit`: Specifies the maximum field size + 1 allowed.
265
+ # Deprecated since 3.2.3. Use `max_field_size` instead.
266
+ # * `max_field_size`: Specifies the maximum field size allowed.
253
267
  # * `converters`: Specifies the field converters to be used.
254
268
  # * `unconverted_fields`: Specifies whether unconverted fields are to be
255
269
  # available.
@@ -1515,7 +1529,7 @@
1515
1529
  # Header converters operate only on headers (and not on other rows).
1516
1530
  #
1517
1531
  # There are three ways to use header converters; these examples use built-in
1518
- # header converter `:dowhcase`, which downcases each parsed header.
1532
+ # header converter `:downcase`, which downcases each parsed header.
1519
1533
  #
1520
1534
  # * Option `header_converters` with a singleton parsing method:
1521
1535
  # string = "Name,Count\nFoo,0\n,Bar,1\nBaz,2"
@@ -1653,119 +1667,90 @@ class CSV < Object
1653
1667
 
1654
1668
  # <!--
1655
1669
  # rdoc-file=lib/csv.rb
1656
- # - foreach(path, mode='r', **options) {|row| ... )
1657
- # - foreach(io, mode='r', **options {|row| ... )
1658
- # - foreach(path, mode='r', headers: ..., **options) {|row| ... )
1659
- # - foreach(io, mode='r', headers: ..., **options {|row| ... )
1660
- # - foreach(path, mode='r', **options) -> new_enumerator
1661
- # - foreach(io, mode='r', **options -> new_enumerator
1670
+ # - foreach(path_or_io, mode='r', **options) {|row| ... )
1671
+ # - foreach(path_or_io, mode='r', **options) -> new_enumerator
1662
1672
  # -->
1663
- # Calls the block with each row read from source `path` or `io`.
1673
+ # Calls the block with each row read from source `path_or_io`.
1664
1674
  #
1665
- # * Argument `path`, if given, must be the path to a file.
1666
- # * Argument `io` should be an IO object that is:
1667
- # * Open for reading; on return, the IO object will be closed.
1668
- # * Positioned at the beginning. To position at the end, for appending,
1669
- # use method CSV.generate. For any other positioning, pass a preset
1670
- # StringIO object instead.
1675
+ # Path input without headers:
1671
1676
  #
1672
- # * Argument `mode`, if given, must be a File mode See [Open
1673
- # Mode](IO.html#method-c-new-label-Open+Mode).
1674
- # * Arguments `**options` must be keyword options. See [Options for
1675
- # Parsing](#class-CSV-label-Options+for+Parsing).
1676
- # * This method optionally accepts an additional `:encoding` option that you
1677
- # can use to specify the Encoding of the data read from `path` or `io`. You
1678
- # must provide this unless your data is in the encoding given by
1679
- # `Encoding::default_external`. Parsing will use this to determine how to
1680
- # parse the data. You may provide a second Encoding to have the data
1681
- # transcoded as it is read. For example,
1682
- # encoding: 'UTF-32BE:UTF-8'
1683
- #
1684
- # would read `UTF-32BE` data from the file but transcode it to `UTF-8`
1685
- # before parsing.
1686
- #
1687
- #
1688
- # ###### Without Option `headers`
1689
- #
1690
- # Without option `headers`, returns each row as an Array object.
1691
- #
1692
- # These examples assume prior execution of:
1693
1677
  # string = "foo,0\nbar,1\nbaz,2\n"
1694
- # path = 't.csv'
1695
- # File.write(path, string)
1696
- #
1697
- # Read rows from a file at `path`:
1698
- # CSV.foreach(path) {|row| p row }
1678
+ # in_path = 't.csv'
1679
+ # File.write(in_path, string)
1680
+ # CSV.foreach(in_path) {|row| p row }
1699
1681
  #
1700
1682
  # Output:
1701
- # ["foo", "0"]
1702
- # ["bar", "1"]
1703
- # ["baz", "2"]
1704
- #
1705
- # Read rows from an IO object:
1706
- # File.open(path) do |file|
1707
- # CSV.foreach(file) {|row| p row }
1708
- # end
1709
1683
  #
1710
- # Output:
1711
1684
  # ["foo", "0"]
1712
1685
  # ["bar", "1"]
1713
1686
  # ["baz", "2"]
1714
1687
  #
1715
- # Returns a new Enumerator if no block given:
1716
- # CSV.foreach(path) # => #<Enumerator: CSV:foreach("t.csv", "r")>
1717
- # CSV.foreach(File.open(path)) # => #<Enumerator: CSV:foreach(#<File:t.csv>, "r")>
1688
+ # Path input with headers:
1718
1689
  #
1719
- # Issues a warning if an encoding is unsupported:
1720
- # CSV.foreach(File.open(path), encoding: 'foo:bar') {|row| }
1690
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
1691
+ # in_path = 't.csv'
1692
+ # File.write(in_path, string)
1693
+ # CSV.foreach(in_path, headers: true) {|row| p row }
1721
1694
  #
1722
1695
  # Output:
1723
- # warning: Unsupported encoding foo ignored
1724
- # warning: Unsupported encoding bar ignored
1725
1696
  #
1726
- # ###### With Option `headers`
1697
+ # <CSV::Row "Name":"foo" "Value":"0">
1698
+ # <CSV::Row "Name":"bar" "Value":"1">
1699
+ # <CSV::Row "Name":"baz" "Value":"2">
1727
1700
  #
1728
- # With {option `headers`[}](#class-CSV-label-Option+headers), returns each row
1729
- # as a CSV::Row object.
1701
+ # IO stream input without headers:
1730
1702
  #
1731
- # These examples assume prior execution of:
1732
- # string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n"
1703
+ # string = "foo,0\nbar,1\nbaz,2\n"
1733
1704
  # path = 't.csv'
1734
1705
  # File.write(path, string)
1735
- #
1736
- # Read rows from a file at `path`:
1737
- # CSV.foreach(path, headers: true) {|row| p row }
1706
+ # File.open('t.csv') do |in_io|
1707
+ # CSV.foreach(in_io) {|row| p row }
1708
+ # end
1738
1709
  #
1739
1710
  # Output:
1740
- # #<CSV::Row "Name":"foo" "Count":"0">
1741
- # #<CSV::Row "Name":"bar" "Count":"1">
1742
- # #<CSV::Row "Name":"baz" "Count":"2">
1743
1711
  #
1744
- # Read rows from an IO object:
1745
- # File.open(path) do |file|
1746
- # CSV.foreach(file, headers: true) {|row| p row }
1712
+ # ["foo", "0"]
1713
+ # ["bar", "1"]
1714
+ # ["baz", "2"]
1715
+ #
1716
+ # IO stream input with headers:
1717
+ #
1718
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
1719
+ # path = 't.csv'
1720
+ # File.write(path, string)
1721
+ # File.open('t.csv') do |in_io|
1722
+ # CSV.foreach(in_io, headers: true) {|row| p row }
1747
1723
  # end
1748
1724
  #
1749
1725
  # Output:
1750
- # #<CSV::Row "Name":"foo" "Count":"0">
1751
- # #<CSV::Row "Name":"bar" "Count":"1">
1752
- # #<CSV::Row "Name":"baz" "Count":"2">
1753
1726
  #
1754
- # ---
1727
+ # <CSV::Row "Name":"foo" "Value":"0">
1728
+ # <CSV::Row "Name":"bar" "Value":"1">
1729
+ # <CSV::Row "Name":"baz" "Value":"2">
1755
1730
  #
1756
- # Raises an exception if `path` is a String, but not the path to a readable
1757
- # file:
1758
- # # Raises Errno::ENOENT (No such file or directory @ rb_sysopen - nosuch.csv):
1759
- # CSV.foreach('nosuch.csv') {|row| }
1731
+ # With no block given, returns an Enumerator:
1760
1732
  #
1761
- # Raises an exception if `io` is an IO object, but not open for reading:
1762
- # io = File.open(path, 'w') {|row| }
1763
- # # Raises TypeError (no implicit conversion of nil into String):
1764
- # CSV.foreach(io) {|row| }
1733
+ # string = "foo,0\nbar,1\nbaz,2\n"
1734
+ # path = 't.csv'
1735
+ # File.write(path, string)
1736
+ # CSV.foreach(path) # => #<Enumerator: CSV:foreach("t.csv", "r")>
1737
+ #
1738
+ # Arguments:
1739
+ # * Argument `path_or_io` must be a file path or an IO stream.
1740
+ # * Argument `mode`, if given, must be a File mode See [Open
1741
+ # Mode](https://ruby-doc.org/core/IO.html#method-c-new-label-Open+Mode).
1742
+ # * Arguments `**options` must be keyword options. See [Options for
1743
+ # Parsing](#class-CSV-label-Options+for+Parsing).
1744
+ # * This method optionally accepts an additional `:encoding` option that you
1745
+ # can use to specify the Encoding of the data read from `path` or `io`. You
1746
+ # must provide this unless your data is in the encoding given by
1747
+ # `Encoding::default_external`. Parsing will use this to determine how to
1748
+ # parse the data. You may provide a second Encoding to have the data
1749
+ # transcoded as it is read. For example,
1750
+ # encoding: 'UTF-32BE:UTF-8'
1765
1751
  #
1766
- # Raises an exception if `mode` is invalid:
1767
- # # Raises ArgumentError (invalid access mode nosuch):
1768
- # CSV.foreach(path, 'nosuch') {|row| }
1752
+ # would read `UTF-32BE` data from the file but transcode it to `UTF-8`
1753
+ # before parsing.
1769
1754
  #
1770
1755
  def self.foreach: [U] (String | IO | StringIO path, ?::Hash[Symbol, U] options) { (::Array[String?] arg0) -> void } -> void
1771
1756
 
@@ -2250,12 +2235,80 @@ CSV::DEFAULT_OPTIONS: ::Hash[untyped, untyped]
2250
2235
  CSV::VERSION: String
2251
2236
 
2252
2237
  # <!-- rdoc-file=lib/csv/row.rb -->
2253
- # A CSV::Row is part Array and part Hash. It retains an order for the fields and
2254
- # allows duplicates just as an Array would, but also allows you to access fields
2255
- # by name just as you could if they were in a Hash.
2238
+ # # CSV::Row
2239
+ # A CSV::Row instance represents a CSV table row. (see [class
2240
+ # CSV](../CSV.html)).
2241
+ #
2242
+ # The instance may have:
2243
+ # * Fields: each is an object, not necessarily a String.
2244
+ # * Headers: each serves a key, and also need not be a String.
2245
+ #
2246
+ #
2247
+ # ### Instance Methods
2248
+ #
2249
+ # CSV::Row has three groups of instance methods:
2250
+ # * Its own internally defined instance methods.
2251
+ # * Methods included by module Enumerable.
2252
+ # * Methods delegated to class Array.:
2253
+ # * Array#empty?
2254
+ # * Array#length
2255
+ # * Array#size
2256
+ #
2257
+ #
2258
+ #
2259
+ # ## Creating a CSV::Row Instance
2260
+ #
2261
+ # Commonly, a new CSV::Row instance is created by parsing CSV source that has
2262
+ # headers:
2263
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2264
+ # table = CSV.parse(source, headers: true)
2265
+ # table.each {|row| p row }
2266
+ #
2267
+ # Output:
2268
+ # #<CSV::Row "Name":"foo" "Value":"0">
2269
+ # #<CSV::Row "Name":"bar" "Value":"1">
2270
+ # #<CSV::Row "Name":"baz" "Value":"2">
2271
+ #
2272
+ # You can also create a row directly. See ::new.
2273
+ #
2274
+ # ## Headers
2275
+ #
2276
+ # Like a CSV::Table, a CSV::Row has headers.
2256
2277
  #
2257
- # All rows returned by CSV will be constructed from this class, if header row
2258
- # processing is activated.
2278
+ # A CSV::Row that was created by parsing CSV source inherits its headers from
2279
+ # the table:
2280
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2281
+ # table = CSV.parse(source, headers: true)
2282
+ # row = table.first
2283
+ # row.headers # => ["Name", "Value"]
2284
+ #
2285
+ # You can also create a new row with headers; like the keys in a Hash, the
2286
+ # headers need not be Strings:
2287
+ # row = CSV::Row.new([:name, :value], ['foo', 0])
2288
+ # row.headers # => [:name, :value]
2289
+ #
2290
+ # The new row retains its headers even if added to a table that has headers:
2291
+ # table << row # => #<CSV::Table mode:col_or_row row_count:5>
2292
+ # row.headers # => [:name, :value]
2293
+ # row[:name] # => "foo"
2294
+ # row['Name'] # => nil
2295
+ #
2296
+ # ## Accessing Fields
2297
+ #
2298
+ # You may access a field in a CSV::Row with either its Integer index
2299
+ # (Array-style) or its header (Hash-style).
2300
+ #
2301
+ # Fetch a field using method #[]:
2302
+ # row = CSV::Row.new(['Name', 'Value'], ['foo', 0])
2303
+ # row[1] # => 0
2304
+ # row['Value'] # => 0
2305
+ #
2306
+ # Set a field using method #[]=:
2307
+ # row = CSV::Row.new(['Name', 'Value'], ['foo', 0])
2308
+ # row # => #<CSV::Row "Name":"foo" "Value":0>
2309
+ # row[0] = 'bar'
2310
+ # row['Value'] = 1
2311
+ # row # => #<CSV::Row "Name":"bar" "Value":1>
2259
2312
  #
2260
2313
  class CSV::Row < Object
2261
2314
  include Enumerable[Array[String]]
@@ -2296,10 +2349,17 @@ class CSV::Row < Object
2296
2349
 
2297
2350
  # <!--
2298
2351
  # rdoc-file=lib/csv/row.rb
2299
- # - ==(other)
2352
+ # - row == other -> true or false
2300
2353
  # -->
2301
- # Returns `true` if this row contains the same headers and fields in the same
2302
- # order as `other`.
2354
+ # Returns `true` if `other` is a /CSV::Row that has the same fields (headers and
2355
+ # values) in the same order as `self`; otherwise returns `false`:
2356
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
2357
+ # table = CSV.parse(source, headers: true)
2358
+ # row = table[0]
2359
+ # other_row = table[0]
2360
+ # row == other_row # => true
2361
+ # other_row = table[1]
2362
+ # row == other_row # => false
2303
2363
  #
2304
2364
  def ==: (untyped other) -> bool
2305
2365
 
@@ -2428,7 +2488,7 @@ class CSV::Row < Object
2428
2488
  # `index_or_header` and `specifiers`.
2429
2489
  #
2430
2490
  # The nested objects may be instances of various classes. See [Dig
2431
- # Methods](https://docs.ruby-lang.org/en/master/doc/dig_methods_rdoc.html).
2491
+ # Methods](https://docs.ruby-lang.org/en/master/dig_methods_rdoc.html).
2432
2492
  #
2433
2493
  # Examples:
2434
2494
  # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
@@ -2442,14 +2502,21 @@ class CSV::Row < Object
2442
2502
 
2443
2503
  # <!--
2444
2504
  # rdoc-file=lib/csv/row.rb
2445
- # - each(&block)
2505
+ # - row.each {|header, value| ... } -> self
2446
2506
  # -->
2447
- # Yields each pair of the row as header and field tuples (much like iterating
2448
- # over a Hash). This method returns the row for chaining.
2507
+ # Calls the block with each header-value pair; returns `self`:
2508
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
2509
+ # table = CSV.parse(source, headers: true)
2510
+ # row = table[0]
2511
+ # row.each {|header, value| p [header, value] }
2449
2512
  #
2450
- # If no block is given, an Enumerator is returned.
2513
+ # Output:
2514
+ # ["Name", "Foo"]
2515
+ # ["Name", "Bar"]
2516
+ # ["Name", "Baz"]
2451
2517
  #
2452
- # Support for Enumerable.
2518
+ # If no block is given, returns a new Enumerator:
2519
+ # row.each # => #<Enumerator: #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz">:each>
2453
2520
  #
2454
2521
  def each: () -> Enumerator[Array[String], self]
2455
2522
  | () { (Array[String]) -> void } -> self
@@ -2465,9 +2532,9 @@ class CSV::Row < Object
2465
2532
 
2466
2533
  # <!--
2467
2534
  # rdoc-file=lib/csv/row.rb
2468
- # - fetch(header)
2469
- # - fetch(header, default)
2470
- # - fetch(header) {|row| ... }
2535
+ # - fetch(header) -> value
2536
+ # - fetch(header, default) -> value
2537
+ # - fetch(header) {|row| ... } -> value
2471
2538
  # -->
2472
2539
  # Returns the field value as specified by `header`.
2473
2540
  #
@@ -2507,9 +2574,9 @@ class CSV::Row < Object
2507
2574
 
2508
2575
  # <!--
2509
2576
  # rdoc-file=lib/csv/row.rb
2510
- # - field(index)
2511
- # - field(header)
2512
- # - field(header, offset)
2577
+ # - field(index) -> value
2578
+ # - field(header) -> value
2579
+ # - field(header, offset) -> value
2513
2580
  # -->
2514
2581
  # Returns the field value for the given `index` or `header`.
2515
2582
  #
@@ -2550,9 +2617,14 @@ class CSV::Row < Object
2550
2617
 
2551
2618
  # <!--
2552
2619
  # rdoc-file=lib/csv/row.rb
2553
- # - field?(data)
2620
+ # - row.field?(value) -> true or false
2554
2621
  # -->
2555
- # Returns `true` if `data` matches a field in this row, and `false` otherwise.
2622
+ # Returns `true` if `value` is a field in this row, `false` otherwise:
2623
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
2624
+ # table = CSV.parse(source, headers: true)
2625
+ # row = table[0]
2626
+ # row.field?('Bar') # => true
2627
+ # row.field?('BAR') # => false
2556
2628
  #
2557
2629
  def field?: (untyped data) -> bool
2558
2630
 
@@ -2566,7 +2638,7 @@ class CSV::Row < Object
2566
2638
 
2567
2639
  # <!--
2568
2640
  # rdoc-file=lib/csv/row.rb
2569
- # - self.fields(*specifiers)
2641
+ # - self.fields(*specifiers) -> array_of_fields
2570
2642
  # -->
2571
2643
  # Returns field values per the given `specifiers`, which may be any mixture of:
2572
2644
  # * Integer index.
@@ -2613,7 +2685,7 @@ class CSV::Row < Object
2613
2685
 
2614
2686
  # <!--
2615
2687
  # rdoc-file=lib/csv/row.rb
2616
- # - row.has_key?(header)
2688
+ # - row.has_key?(header) -> true or false
2617
2689
  # -->
2618
2690
  # Returns `true` if there is a field with the given `header`, `false` otherwise.
2619
2691
  #
@@ -2636,7 +2708,7 @@ class CSV::Row < Object
2636
2708
 
2637
2709
  # <!--
2638
2710
  # rdoc-file=lib/csv/row.rb
2639
- # - row.headers
2711
+ # - row.headers -> array_of_headers
2640
2712
  # -->
2641
2713
  # Returns the headers for this row:
2642
2714
  # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
@@ -2655,12 +2727,23 @@ class CSV::Row < Object
2655
2727
 
2656
2728
  # <!--
2657
2729
  # rdoc-file=lib/csv/row.rb
2658
- # - index( header )
2659
- # - index( header, offset )
2730
+ # - index(header) -> index
2731
+ # - index(header, offset) -> index
2660
2732
  # -->
2661
- # This method will return the index of a field with the provided `header`. The
2662
- # `offset` can be used to locate duplicate header names, as described in
2663
- # CSV::Row.field().
2733
+ # Returns the index for the given header, if it exists; otherwise returns `nil`.
2734
+ #
2735
+ # With the single argument `header`, returns the index of the first-found field
2736
+ # with the given `header`:
2737
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
2738
+ # table = CSV.parse(source, headers: true)
2739
+ # row = table[0]
2740
+ # row.index('Name') # => 0
2741
+ # row.index('NAME') # => nil
2742
+ #
2743
+ # With arguments `header` and `offset`, returns the index of the first-found
2744
+ # field with given `header`, but ignoring the first `offset` fields:
2745
+ # row.index('Name', 1) # => 1
2746
+ # row.index('Name', 3) # => nil
2664
2747
  #
2665
2748
  def index: (untyped header, ?untyped minimum_index) -> untyped
2666
2749
 
@@ -2698,7 +2781,7 @@ class CSV::Row < Object
2698
2781
 
2699
2782
  # <!--
2700
2783
  # rdoc-file=lib/csv/row.rb
2701
- # - row.push(*values) ->self
2784
+ # - row.push(*values) -> self
2702
2785
  # -->
2703
2786
  # Appends each of the given `values` to `self` as a field; returns `self`:
2704
2787
  # source = "Name,Name,Name\nFoo,Bar,Baz\n"
@@ -2774,12 +2857,149 @@ class CSV::MalformedCSVError < RuntimeError
2774
2857
  end
2775
2858
 
2776
2859
  # <!-- rdoc-file=lib/csv/table.rb -->
2777
- # A CSV::Table is a two-dimensional data structure for representing CSV
2778
- # documents. Tables allow you to work with the data by row or column, manipulate
2779
- # the data, and even convert the results back to CSV, if needed.
2860
+ # # CSV::Table
2861
+ # A CSV::Table instance represents CSV data. (see [class CSV](../CSV.html)).
2862
+ #
2863
+ # The instance may have:
2864
+ # * Rows: each is a Table::Row object.
2865
+ # * Headers: names for the columns.
2866
+ #
2867
+ #
2868
+ # ### Instance Methods
2869
+ #
2870
+ # CSV::Table has three groups of instance methods:
2871
+ # * Its own internally defined instance methods.
2872
+ # * Methods included by module Enumerable.
2873
+ # * Methods delegated to class Array.:
2874
+ # * Array#empty?
2875
+ # * Array#length
2876
+ # * Array#size
2877
+ #
2878
+ #
2879
+ #
2880
+ # ## Creating a CSV::Table Instance
2780
2881
  #
2781
- # All tables returned by CSV will be constructed from this class, if header row
2782
- # processing is activated.
2882
+ # Commonly, a new CSV::Table instance is created by parsing CSV source using
2883
+ # headers:
2884
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2885
+ # table = CSV.parse(source, headers: true)
2886
+ # table.class # => CSV::Table
2887
+ #
2888
+ # You can also create an instance directly. See ::new.
2889
+ #
2890
+ # ## Headers
2891
+ #
2892
+ # If a table has headers, the headers serve as labels for the columns of data.
2893
+ # Each header serves as the label for its column.
2894
+ #
2895
+ # The headers for a CSV::Table object are stored as an Array of Strings.
2896
+ #
2897
+ # Commonly, headers are defined in the first row of CSV source:
2898
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2899
+ # table = CSV.parse(source, headers: true)
2900
+ # table.headers # => ["Name", "Value"]
2901
+ #
2902
+ # If no headers are defined, the Array is empty:
2903
+ # table = CSV::Table.new([])
2904
+ # table.headers # => []
2905
+ #
2906
+ # ## Access Modes
2907
+ #
2908
+ # CSV::Table provides three modes for accessing table data:
2909
+ # * Row mode.
2910
+ # * Column mode.
2911
+ # * Mixed mode (the default for a new table).
2912
+ #
2913
+ #
2914
+ # The access mode for aCSV::Table instance affects the behavior of some of its
2915
+ # instance methods:
2916
+ # * #[]
2917
+ # * #[]=
2918
+ # * #delete
2919
+ # * #delete_if
2920
+ # * #each
2921
+ # * #values_at
2922
+ #
2923
+ #
2924
+ # ### Row Mode
2925
+ #
2926
+ # Set a table to row mode with method #by_row!:
2927
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2928
+ # table = CSV.parse(source, headers: true)
2929
+ # table.by_row! # => #<CSV::Table mode:row row_count:4>
2930
+ #
2931
+ # Specify a single row by an Integer index:
2932
+ # # Get a row.
2933
+ # table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
2934
+ # # Set a row, then get it.
2935
+ # table[1] = CSV::Row.new(['Name', 'Value'], ['bam', 3])
2936
+ # table[1] # => #<CSV::Row "Name":"bam" "Value":3>
2937
+ #
2938
+ # Specify a sequence of rows by a Range:
2939
+ # # Get rows.
2940
+ # table[1..2] # => [#<CSV::Row "Name":"bam" "Value":3>, #<CSV::Row "Name":"baz" "Value":"2">]
2941
+ # # Set rows, then get them.
2942
+ # table[1..2] = [
2943
+ # CSV::Row.new(['Name', 'Value'], ['bat', 4]),
2944
+ # CSV::Row.new(['Name', 'Value'], ['bad', 5]),
2945
+ # ]
2946
+ # table[1..2] # => [["Name", #<CSV::Row "Name":"bat" "Value":4>], ["Value", #<CSV::Row "Name":"bad" "Value":5>]]
2947
+ #
2948
+ # ### Column Mode
2949
+ #
2950
+ # Set a table to column mode with method #by_col!:
2951
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2952
+ # table = CSV.parse(source, headers: true)
2953
+ # table.by_col! # => #<CSV::Table mode:col row_count:4>
2954
+ #
2955
+ # Specify a column by an Integer index:
2956
+ # # Get a column.
2957
+ # table[0]
2958
+ # # Set a column, then get it.
2959
+ # table[0] = ['FOO', 'BAR', 'BAZ']
2960
+ # table[0] # => ["FOO", "BAR", "BAZ"]
2961
+ #
2962
+ # Specify a column by its String header:
2963
+ # # Get a column.
2964
+ # table['Name'] # => ["FOO", "BAR", "BAZ"]
2965
+ # # Set a column, then get it.
2966
+ # table['Name'] = ['Foo', 'Bar', 'Baz']
2967
+ # table['Name'] # => ["Foo", "Bar", "Baz"]
2968
+ #
2969
+ # ### Mixed Mode
2970
+ #
2971
+ # In mixed mode, you can refer to either rows or columns:
2972
+ # * An Integer index refers to a row.
2973
+ # * A Range index refers to multiple rows.
2974
+ # * A String index refers to a column.
2975
+ #
2976
+ #
2977
+ # Set a table to mixed mode with method #by_col_or_row!:
2978
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2979
+ # table = CSV.parse(source, headers: true)
2980
+ # table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
2981
+ #
2982
+ # Specify a single row by an Integer index:
2983
+ # # Get a row.
2984
+ # table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
2985
+ # # Set a row, then get it.
2986
+ # table[1] = CSV::Row.new(['Name', 'Value'], ['bam', 3])
2987
+ # table[1] # => #<CSV::Row "Name":"bam" "Value":3>
2988
+ #
2989
+ # Specify a sequence of rows by a Range:
2990
+ # # Get rows.
2991
+ # table[1..2] # => [#<CSV::Row "Name":"bam" "Value":3>, #<CSV::Row "Name":"baz" "Value":"2">]
2992
+ # # Set rows, then get them.
2993
+ # table[1] = CSV::Row.new(['Name', 'Value'], ['bat', 4])
2994
+ # table[2] = CSV::Row.new(['Name', 'Value'], ['bad', 5])
2995
+ # table[1..2] # => [["Name", #<CSV::Row "Name":"bat" "Value":4>], ["Value", #<CSV::Row "Name":"bad" "Value":5>]]
2996
+ #
2997
+ # Specify a column by its String header:
2998
+ # # Get a column.
2999
+ # table['Name'] # => ["foo", "bat", "bad"]
3000
+ # # Set a column, then get it.
3001
+ # table['Name'] = ['Foo', 'Bar', 'Baz']
3002
+ # table['Name'] # => ["Foo", "Bar", "Baz"]
2783
3003
  #
2784
3004
  class CSV::Table[out Elem] < Object
2785
3005
  include Enumerable[untyped]
@@ -2787,20 +3007,61 @@ class CSV::Table[out Elem] < Object
2787
3007
 
2788
3008
  # <!--
2789
3009
  # rdoc-file=lib/csv/table.rb
2790
- # - new(array_of_rows, headers: nil)
3010
+ # - CSV::Table.new(array_of_rows, headers = nil) -> csv_table
2791
3011
  # -->
2792
- # Constructs a new CSV::Table from `array_of_rows`, which are expected to be
2793
- # CSV::Row objects. All rows are assumed to have the same headers.
3012
+ # Returns a new CSV::Table object.
2794
3013
  #
2795
- # The optional `headers` parameter can be set to Array of headers. If headers
2796
- # aren't set, headers are fetched from CSV::Row objects. Otherwise, headers()
2797
- # method will return headers being set in headers argument.
3014
+ # * Argument `array_of_rows` must be an Array of CSV::Row objects.
3015
+ # * Argument `headers`, if given, may be an Array of Strings.
2798
3016
  #
2799
- # A CSV::Table object supports the following Array methods through delegation:
2800
3017
  #
2801
- # * empty?()
2802
- # * length()
2803
- # * size()
3018
+ # ---
3019
+ #
3020
+ # Create an empty CSV::Table object:
3021
+ # table = CSV::Table.new([])
3022
+ # table # => #<CSV::Table mode:col_or_row row_count:1>
3023
+ #
3024
+ # Create a non-empty CSV::Table object:
3025
+ # rows = [
3026
+ # CSV::Row.new([], []),
3027
+ # CSV::Row.new([], []),
3028
+ # CSV::Row.new([], []),
3029
+ # ]
3030
+ # table = CSV::Table.new(rows)
3031
+ # table # => #<CSV::Table mode:col_or_row row_count:4>
3032
+ #
3033
+ # ---
3034
+ #
3035
+ # If argument `headers` is an Array of Strings, those Strings become the table's
3036
+ # headers:
3037
+ # table = CSV::Table.new([], headers: ['Name', 'Age'])
3038
+ # table.headers # => ["Name", "Age"]
3039
+ #
3040
+ # If argument `headers` is not given and the table has rows, the headers are
3041
+ # taken from the first row:
3042
+ # rows = [
3043
+ # CSV::Row.new(['Foo', 'Bar'], []),
3044
+ # CSV::Row.new(['foo', 'bar'], []),
3045
+ # CSV::Row.new(['FOO', 'BAR'], []),
3046
+ # ]
3047
+ # table = CSV::Table.new(rows)
3048
+ # table.headers # => ["Foo", "Bar"]
3049
+ #
3050
+ # If argument `headers` is not given and the table is empty (has no rows), the
3051
+ # headers are also empty:
3052
+ # table = CSV::Table.new([])
3053
+ # table.headers # => []
3054
+ #
3055
+ # ---
3056
+ #
3057
+ # Raises an exception if argument `array_of_rows` is not an Array object:
3058
+ # # Raises NoMethodError (undefined method `first' for :foo:Symbol):
3059
+ # CSV::Table.new(:foo)
3060
+ #
3061
+ # Raises an exception if an element of `array_of_rows` is not a CSV::Table
3062
+ # object:
3063
+ # # Raises NoMethodError (undefined method `headers' for :foo:Symbol):
3064
+ # CSV::Table.new([:foo])
2804
3065
  #
2805
3066
  def initialize: (untyped array_of_rows, ?headers: untyped) -> untyped
2806
3067
 
@@ -2823,7 +3084,7 @@ class CSV::Table[out Elem] < Object
2823
3084
 
2824
3085
  # <!--
2825
3086
  # rdoc-file=lib/csv/table.rb
2826
- # - ==(other)
3087
+ # - table == other_table -> true or false
2827
3088
  # -->
2828
3089
  # Returns `true` if all each row of `self` `==` the corresponding row of
2829
3090
  # `other_table`, otherwise, `false`.
@@ -2848,17 +3109,23 @@ class CSV::Table[out Elem] < Object
2848
3109
 
2849
3110
  # <!--
2850
3111
  # rdoc-file=lib/csv/table.rb
2851
- # - table[n] -> row
2852
- # - table[range] -> array_of_rows
2853
- # - table[header] -> array_of_fields
3112
+ # - table[n] -> row or column_data
3113
+ # - table[range] -> array_of_rows or array_of_column_data
3114
+ # - table[header] -> array_of_column_data
2854
3115
  # -->
2855
3116
  # Returns data from the table; does not modify the table.
2856
3117
  #
2857
3118
  # ---
2858
3119
  #
2859
- # The expression `table[n]`, where `n` is a non-negative Integer, returns the
2860
- # +n+th row of the table, if that row exists, and if the access mode is `:row`
2861
- # or `:col_or_row`:
3120
+ #
3121
+ # Fetch a Row by Its Integer Index
3122
+ # :
3123
+ # * Form: `table[n]`, `n` an integer.
3124
+ # * Access mode: `:row` or `:col_or_row`.
3125
+ # * Return value: *nth* row of the table, if that row exists; otherwise `nil`.
3126
+ #
3127
+ #
3128
+ # Returns the *nth* row of the table if that row exists:
2862
3129
  # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2863
3130
  # table = CSV.parse(source, headers: true)
2864
3131
  # table.by_row! # => #<CSV::Table mode:row row_count:4>
@@ -2871,21 +3138,50 @@ class CSV::Table[out Elem] < Object
2871
3138
  #
2872
3139
  # Returns `nil` if `n` is too large or too small:
2873
3140
  # table[4] # => nil
2874
- # table[-4] => nil
3141
+ # table[-4] # => nil
2875
3142
  #
2876
- # Raises an exception if the access mode is `:row` and `n` is not an
2877
- # [Integer-convertible
2878
- # object](https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html#lab
2879
- # el-Integer-Convertible+Objects).
3143
+ # Raises an exception if the access mode is `:row` and `n` is not an Integer:
2880
3144
  # table.by_row! # => #<CSV::Table mode:row row_count:4>
2881
3145
  # # Raises TypeError (no implicit conversion of String into Integer):
2882
3146
  # table['Name']
2883
3147
  #
2884
3148
  # ---
2885
3149
  #
2886
- # The expression `table[range]`, where `range` is a Range object, returns rows
2887
- # from the table, beginning at row `range.first`, if those rows exist, and if
2888
- # the access mode is `:row` or `:col_or_row`:
3150
+ #
3151
+ # Fetch a Column by Its Integer Index
3152
+ # :
3153
+ # * Form: `table[n]`, `n` an Integer.
3154
+ # * Access mode: `:col`.
3155
+ # * Return value: *nth* column of the table, if that column exists; otherwise
3156
+ # an Array of `nil` fields of length `self.size`.
3157
+ #
3158
+ #
3159
+ # Returns the *nth* column of the table if that column exists:
3160
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3161
+ # table = CSV.parse(source, headers: true)
3162
+ # table.by_col! # => #<CSV::Table mode:col row_count:4>
3163
+ # table[1] # => ["0", "1", "2"]
3164
+ #
3165
+ # Counts backward from the last column if `n` is negative:
3166
+ # table[-2] # => ["foo", "bar", "baz"]
3167
+ #
3168
+ # Returns an Array of `nil` fields if `n` is too large or too small:
3169
+ # table[4] # => [nil, nil, nil]
3170
+ # table[-4] # => [nil, nil, nil]
3171
+ #
3172
+ # ---
3173
+ #
3174
+ #
3175
+ # Fetch Rows by Range
3176
+ # :
3177
+ # * Form: `table[range]`, `range` a Range object.
3178
+ # * Access mode: `:row` or `:col_or_row`.
3179
+ # * Return value: rows from the table, beginning at row `range.start`, if
3180
+ # those rows exists.
3181
+ #
3182
+ #
3183
+ # Returns rows from the table, beginning at row `range.first`, if those rows
3184
+ # exist:
2889
3185
  # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2890
3186
  # table = CSV.parse(source, headers: true)
2891
3187
  # table.by_row! # => #<CSV::Table mode:row row_count:4>
@@ -2895,11 +3191,11 @@ class CSV::Table[out Elem] < Object
2895
3191
  # rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
2896
3192
  # rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
2897
3193
  #
2898
- # If there are too few rows, returns all from `range.first` to the end:
3194
+ # If there are too few rows, returns all from `range.start` to the end:
2899
3195
  # rows = table[1..50] # => #<CSV::Row "Name":"bar" "Value":"1">
2900
3196
  # rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
2901
3197
  #
2902
- # Special case: if `range.start == table.size`, returns an empty Array:
3198
+ # Special case: if `range.start == table.size`, returns an empty Array:
2903
3199
  # table[table.size..50] # => []
2904
3200
  #
2905
3201
  # If `range.end` is negative, calculates the ending index from the end:
@@ -2915,9 +3211,47 @@ class CSV::Table[out Elem] < Object
2915
3211
  #
2916
3212
  # ---
2917
3213
  #
2918
- # The expression `table[header]`, where `header` is a String, returns column
2919
- # values (Array of Strings) if the column exists and if the access mode is
2920
- # `:col` or `:col_or_row`:
3214
+ #
3215
+ # Fetch Columns by Range
3216
+ # :
3217
+ # * Form: `table[range]`, `range` a Range object.
3218
+ # * Access mode: `:col`.
3219
+ # * Return value: column data from the table, beginning at column
3220
+ # `range.start`, if those columns exist.
3221
+ #
3222
+ #
3223
+ # Returns column values from the table, if the column exists; the values are
3224
+ # arranged by row:
3225
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3226
+ # table = CSV.parse(source, headers: true)
3227
+ # table.by_col!
3228
+ # table[0..1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
3229
+ #
3230
+ # Special case: if `range.start == headers.size`, returns an Array (size:
3231
+ # `table.size`) of empty Arrays:
3232
+ # table[table.headers.size..50] # => [[], [], []]
3233
+ #
3234
+ # If `range.end` is negative, calculates the ending index from the end:
3235
+ # table[0..-1] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
3236
+ #
3237
+ # If `range.start` is negative, calculates the starting index from the end:
3238
+ # table[-2..2] # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
3239
+ #
3240
+ # If `range.start` is larger than `table.size`, returns an Array of `nil`
3241
+ # values:
3242
+ # table[4..4] # => [nil, nil, nil]
3243
+ #
3244
+ # ---
3245
+ #
3246
+ #
3247
+ # Fetch a Column by Its String Header
3248
+ # :
3249
+ # * Form: `table[header]`, `header` a String header.
3250
+ # * Access mode: `:col` or `:col_or_row`
3251
+ # * Return value: column data from the table, if that `header` exists.
3252
+ #
3253
+ #
3254
+ # Returns column values from the table, if the column exists:
2921
3255
  # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
2922
3256
  # table = CSV.parse(source, headers: true)
2923
3257
  # table.by_col! # => #<CSV::Table mode:col row_count:4>
@@ -2938,96 +3272,248 @@ class CSV::Table[out Elem] < Object
2938
3272
 
2939
3273
  # <!--
2940
3274
  # rdoc-file=lib/csv/table.rb
2941
- # - []=(index_or_header, value)
3275
+ # - table[n] = row -> row
3276
+ # - table[n] = field_or_array_of_fields -> field_or_array_of_fields
3277
+ # - table[header] = field_or_array_of_fields -> field_or_array_of_fields
2942
3278
  # -->
2943
- # In the default mixed mode, this method assigns rows for index access and
2944
- # columns for header access. You can force the index association by first
2945
- # calling by_col!() or by_row!().
3279
+ # Puts data onto the table.
3280
+ #
3281
+ # ---
3282
+ #
3283
+ #
3284
+ # Set a Row by Its Integer Index
3285
+ # :
3286
+ # * Form: `table[n] = row`, `n` an Integer, `row` a CSV::Row instance or an
3287
+ # Array of fields.
3288
+ # * Access mode: `:row` or `:col_or_row`.
3289
+ # * Return value: `row`.
2946
3290
  #
2947
- # Rows may be set to an Array of values (which will inherit the table's
2948
- # headers()) or a CSV::Row.
2949
3291
  #
2950
- # Columns may be set to a single value, which is copied to each row of the
2951
- # column, or an Array of values. Arrays of values are assigned to rows top to
2952
- # bottom in row major order. Excess values are ignored and if the Array does not
2953
- # have a value for each row the extra rows will receive a `nil`.
3292
+ # If the row exists, it is replaced:
3293
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3294
+ # table = CSV.parse(source, headers: true)
3295
+ # new_row = CSV::Row.new(['Name', 'Value'], ['bat', 3])
3296
+ # table.by_row! # => #<CSV::Table mode:row row_count:4>
3297
+ # return_value = table[0] = new_row
3298
+ # return_value.equal?(new_row) # => true # Returned the row
3299
+ # table[0].to_h # => {"Name"=>"bat", "Value"=>3}
2954
3300
  #
2955
- # Assigning to an existing column or row clobbers the data. Assigning to new
2956
- # columns creates them at the right end of the table.
3301
+ # With access mode `:col_or_row`:
3302
+ # table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
3303
+ # table[0] = CSV::Row.new(['Name', 'Value'], ['bam', 4])
3304
+ # table[0].to_h # => {"Name"=>"bam", "Value"=>4}
3305
+ #
3306
+ # With an Array instead of a CSV::Row, inherits headers from the table:
3307
+ # array = ['bad', 5]
3308
+ # return_value = table[0] = array
3309
+ # return_value.equal?(array) # => true # Returned the array
3310
+ # table[0].to_h # => {"Name"=>"bad", "Value"=>5}
3311
+ #
3312
+ # If the row does not exist, extends the table by adding rows: assigns rows with
3313
+ # `nil` as needed:
3314
+ # table.size # => 3
3315
+ # table[5] = ['bag', 6]
3316
+ # table.size # => 6
3317
+ # table[3] # => nil
3318
+ # table[4]# => nil
3319
+ # table[5].to_h # => {"Name"=>"bag", "Value"=>6}
3320
+ #
3321
+ # Note that the `nil` rows are actually `nil`, not a row of `nil` fields.
3322
+ #
3323
+ # ---
3324
+ #
3325
+ #
3326
+ # Set a Column by Its Integer Index
3327
+ # :
3328
+ # * Form: `table[n] = array_of_fields`, `n` an Integer, `array_of_fields` an
3329
+ # Array of String fields.
3330
+ # * Access mode: `:col`.
3331
+ # * Return value: `array_of_fields`.
3332
+ #
3333
+ #
3334
+ # If the column exists, it is replaced:
3335
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3336
+ # table = CSV.parse(source, headers: true)
3337
+ # new_col = [3, 4, 5]
3338
+ # table.by_col! # => #<CSV::Table mode:col row_count:4>
3339
+ # return_value = table[1] = new_col
3340
+ # return_value.equal?(new_col) # => true # Returned the column
3341
+ # table[1] # => [3, 4, 5]
3342
+ # # The rows, as revised:
3343
+ # table.by_row! # => #<CSV::Table mode:row row_count:4>
3344
+ # table[0].to_h # => {"Name"=>"foo", "Value"=>3}
3345
+ # table[1].to_h # => {"Name"=>"bar", "Value"=>4}
3346
+ # table[2].to_h # => {"Name"=>"baz", "Value"=>5}
3347
+ # table.by_col! # => #<CSV::Table mode:col row_count:4>
3348
+ #
3349
+ # If there are too few values, fills with `nil` values:
3350
+ # table[1] = [0]
3351
+ # table[1] # => [0, nil, nil]
3352
+ #
3353
+ # If there are too many values, ignores the extra values:
3354
+ # table[1] = [0, 1, 2, 3, 4]
3355
+ # table[1] # => [0, 1, 2]
3356
+ #
3357
+ # If a single value is given, replaces all fields in the column with that value:
3358
+ # table[1] = 'bat'
3359
+ # table[1] # => ["bat", "bat", "bat"]
3360
+ #
3361
+ # ---
3362
+ #
3363
+ #
3364
+ # Set a Column by Its String Header
3365
+ # :
3366
+ # * Form: `table[header] = field_or_array_of_fields`, `header` a String
3367
+ # header, `field_or_array_of_fields` a field value or an Array of String
3368
+ # fields.
3369
+ # * Access mode: `:col` or `:col_or_row`.
3370
+ # * Return value: `field_or_array_of_fields`.
3371
+ #
3372
+ #
3373
+ # If the column exists, it is replaced:
3374
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3375
+ # table = CSV.parse(source, headers: true)
3376
+ # new_col = [3, 4, 5]
3377
+ # table.by_col! # => #<CSV::Table mode:col row_count:4>
3378
+ # return_value = table['Value'] = new_col
3379
+ # return_value.equal?(new_col) # => true # Returned the column
3380
+ # table['Value'] # => [3, 4, 5]
3381
+ # # The rows, as revised:
3382
+ # table.by_row! # => #<CSV::Table mode:row row_count:4>
3383
+ # table[0].to_h # => {"Name"=>"foo", "Value"=>3}
3384
+ # table[1].to_h # => {"Name"=>"bar", "Value"=>4}
3385
+ # table[2].to_h # => {"Name"=>"baz", "Value"=>5}
3386
+ # table.by_col! # => #<CSV::Table mode:col row_count:4>
3387
+ #
3388
+ # If there are too few values, fills with `nil` values:
3389
+ # table['Value'] = [0]
3390
+ # table['Value'] # => [0, nil, nil]
3391
+ #
3392
+ # If there are too many values, ignores the extra values:
3393
+ # table['Value'] = [0, 1, 2, 3, 4]
3394
+ # table['Value'] # => [0, 1, 2]
3395
+ #
3396
+ # If the column does not exist, extends the table by adding columns:
3397
+ # table['Note'] = ['x', 'y', 'z']
3398
+ # table['Note'] # => ["x", "y", "z"]
3399
+ # # The rows, as revised:
3400
+ # table.by_row!
3401
+ # table[0].to_h # => {"Name"=>"foo", "Value"=>0, "Note"=>"x"}
3402
+ # table[1].to_h # => {"Name"=>"bar", "Value"=>1, "Note"=>"y"}
3403
+ # table[2].to_h # => {"Name"=>"baz", "Value"=>2, "Note"=>"z"}
3404
+ # table.by_col!
3405
+ #
3406
+ # If a single value is given, replaces all fields in the column with that value:
3407
+ # table['Value'] = 'bat'
3408
+ # table['Value'] # => ["bat", "bat", "bat"]
2957
3409
  #
2958
3410
  def []=: (untyped index_or_header, untyped value) -> untyped
2959
3411
 
2960
3412
  # <!--
2961
3413
  # rdoc-file=lib/csv/table.rb
2962
- # - by_col()
3414
+ # - table.by_col -> table_dup
2963
3415
  # -->
2964
- # Returns a duplicate table object, in column mode. This is handy for chaining
2965
- # in a single call without changing the table mode, but be aware that this
2966
- # method can consume a fair amount of memory for bigger data sets.
3416
+ # Returns a duplicate of `self`, in column mode (see [Column
3417
+ # Mode](#class-CSV::Table-label-Column+Mode)):
3418
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3419
+ # table = CSV.parse(source, headers: true)
3420
+ # table.mode # => :col_or_row
3421
+ # dup_table = table.by_col
3422
+ # dup_table.mode # => :col
3423
+ # dup_table.equal?(table) # => false # It's a dup
3424
+ #
3425
+ # This may be used to chain method calls without changing the mode (but also
3426
+ # will affect performance and memory usage):
3427
+ # dup_table.by_col['Name']
2967
3428
  #
2968
- # This method returns the duplicate table for chaining. Don't chain destructive
2969
- # methods (like []=()) this way though, since you are working with a duplicate.
3429
+ # Also note that changes to the duplicate table will not affect the original.
2970
3430
  #
2971
3431
  def by_col: () -> untyped
2972
3432
 
2973
3433
  # <!--
2974
3434
  # rdoc-file=lib/csv/table.rb
2975
- # - by_col!()
3435
+ # - table.by_col! -> self
2976
3436
  # -->
2977
- # Switches the mode of this table to column mode. All calls to indexing and
2978
- # iteration methods will work with columns until the mode is changed again.
2979
- #
2980
- # This method returns the table and is safe to chain.
3437
+ # Sets the mode for `self` to column mode (see [Column
3438
+ # Mode](#class-CSV::Table-label-Column+Mode)); returns `self`:
3439
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3440
+ # table = CSV.parse(source, headers: true)
3441
+ # table.mode # => :col_or_row
3442
+ # table1 = table.by_col!
3443
+ # table.mode # => :col
3444
+ # table1.equal?(table) # => true # Returned self
2981
3445
  #
2982
3446
  def by_col!: () -> untyped
2983
3447
 
2984
3448
  # <!--
2985
3449
  # rdoc-file=lib/csv/table.rb
2986
- # - by_col_or_row()
3450
+ # - table.by_col_or_row -> table_dup
2987
3451
  # -->
2988
- # Returns a duplicate table object, in mixed mode. This is handy for chaining in
2989
- # a single call without changing the table mode, but be aware that this method
2990
- # can consume a fair amount of memory for bigger data sets.
3452
+ # Returns a duplicate of `self`, in mixed mode (see [Mixed
3453
+ # Mode](#class-CSV::Table-label-Mixed+Mode)):
3454
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3455
+ # table = CSV.parse(source, headers: true).by_col!
3456
+ # table.mode # => :col
3457
+ # dup_table = table.by_col_or_row
3458
+ # dup_table.mode # => :col_or_row
3459
+ # dup_table.equal?(table) # => false # It's a dup
2991
3460
  #
2992
- # This method returns the duplicate table for chaining. Don't chain destructive
2993
- # methods (like []=()) this way though, since you are working with a duplicate.
3461
+ # This may be used to chain method calls without changing the mode (but also
3462
+ # will affect performance and memory usage):
3463
+ # dup_table.by_col_or_row['Name']
3464
+ #
3465
+ # Also note that changes to the duplicate table will not affect the original.
2994
3466
  #
2995
3467
  def by_col_or_row: () -> untyped
2996
3468
 
2997
3469
  # <!--
2998
3470
  # rdoc-file=lib/csv/table.rb
2999
- # - by_col_or_row!()
3471
+ # - table.by_col_or_row! -> self
3000
3472
  # -->
3001
- # Switches the mode of this table to mixed mode. All calls to indexing and
3002
- # iteration methods will use the default intelligent indexing system until the
3003
- # mode is changed again. In mixed mode an index is assumed to be a row reference
3004
- # while anything else is assumed to be column access by headers.
3005
- #
3006
- # This method returns the table and is safe to chain.
3473
+ # Sets the mode for `self` to mixed mode (see [Mixed
3474
+ # Mode](#class-CSV::Table-label-Mixed+Mode)); returns `self`:
3475
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3476
+ # table = CSV.parse(source, headers: true).by_col!
3477
+ # table.mode # => :col
3478
+ # table1 = table.by_col_or_row!
3479
+ # table.mode # => :col_or_row
3480
+ # table1.equal?(table) # => true # Returned self
3007
3481
  #
3008
3482
  def by_col_or_row!: () -> untyped
3009
3483
 
3010
3484
  # <!--
3011
3485
  # rdoc-file=lib/csv/table.rb
3012
- # - by_row()
3486
+ # - table.by_row -> table_dup
3013
3487
  # -->
3014
- # Returns a duplicate table object, in row mode. This is handy for chaining in
3015
- # a single call without changing the table mode, but be aware that this method
3016
- # can consume a fair amount of memory for bigger data sets.
3488
+ # Returns a duplicate of `self`, in row mode (see [Row
3489
+ # Mode](#class-CSV::Table-label-Row+Mode)):
3490
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3491
+ # table = CSV.parse(source, headers: true)
3492
+ # table.mode # => :col_or_row
3493
+ # dup_table = table.by_row
3494
+ # dup_table.mode # => :row
3495
+ # dup_table.equal?(table) # => false # It's a dup
3496
+ #
3497
+ # This may be used to chain method calls without changing the mode (but also
3498
+ # will affect performance and memory usage):
3499
+ # dup_table.by_row[1]
3017
3500
  #
3018
- # This method returns the duplicate table for chaining. Don't chain destructive
3019
- # methods (like []=()) this way though, since you are working with a duplicate.
3501
+ # Also note that changes to the duplicate table will not affect the original.
3020
3502
  #
3021
3503
  def by_row: () -> untyped
3022
3504
 
3023
3505
  # <!--
3024
3506
  # rdoc-file=lib/csv/table.rb
3025
- # - by_row!()
3507
+ # - table.by_row! -> self
3026
3508
  # -->
3027
- # Switches the mode of this table to row mode. All calls to indexing and
3028
- # iteration methods will work with rows until the mode is changed again.
3029
- #
3030
- # This method returns the table and is safe to chain.
3509
+ # Sets the mode for `self` to row mode (see [Row
3510
+ # Mode](#class-CSV::Table-label-Row+Mode)); returns `self`:
3511
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3512
+ # table = CSV.parse(source, headers: true)
3513
+ # table.mode # => :col_or_row
3514
+ # table1 = table.by_row!
3515
+ # table.mode # => :row
3516
+ # table1.equal?(table) # => true # Returned self
3031
3517
  #
3032
3518
  def by_row!: () -> untyped
3033
3519
 
@@ -3076,7 +3562,7 @@ class CSV::Table[out Elem] < Object
3076
3562
 
3077
3563
  # <!--
3078
3564
  # rdoc-file=lib/csv/table.rb
3079
- # - delete_if() { |header, self| ... }
3565
+ # - table.delete_if {|row_or_column| ... } -> self
3080
3566
  # -->
3081
3567
  # Removes rows or columns for which the block returns a truthy value; returns
3082
3568
  # `self`.
@@ -3119,7 +3605,7 @@ class CSV::Table[out Elem] < Object
3119
3605
 
3120
3606
  # <!--
3121
3607
  # rdoc-file=lib/csv/table.rb
3122
- # - each() { |header, self| ... }
3608
+ # - table.each {|row_or_column| ... ) -> self
3123
3609
  # -->
3124
3610
  # Calls the block with each row or column; returns `self`.
3125
3611
  #
@@ -3155,19 +3641,43 @@ class CSV::Table[out Elem] < Object
3155
3641
 
3156
3642
  # <!--
3157
3643
  # rdoc-file=lib/csv/table.rb
3158
- # - headers()
3644
+ # - table.headers -> array_of_headers
3159
3645
  # -->
3160
- # Returns the headers for the first row of this table (assumed to match all
3161
- # other rows). The headers Array passed to CSV::Table.new is returned for empty
3162
- # tables.
3646
+ # Returns a new Array containing the String headers for the table.
3647
+ #
3648
+ # If the table is not empty, returns the headers from the first row:
3649
+ # rows = [
3650
+ # CSV::Row.new(['Foo', 'Bar'], []),
3651
+ # CSV::Row.new(['FOO', 'BAR'], []),
3652
+ # CSV::Row.new(['foo', 'bar'], []),
3653
+ # ]
3654
+ # table = CSV::Table.new(rows)
3655
+ # table.headers # => ["Foo", "Bar"]
3656
+ # table.delete(0)
3657
+ # table.headers # => ["FOO", "BAR"]
3658
+ # table.delete(0)
3659
+ # table.headers # => ["foo", "bar"]
3660
+ #
3661
+ # If the table is empty, returns a copy of the headers in the table itself:
3662
+ # table.delete(0)
3663
+ # table.headers # => ["Foo", "Bar"]
3163
3664
  #
3164
3665
  def headers: () -> untyped
3165
3666
 
3166
3667
  # <!--
3167
3668
  # rdoc-file=lib/csv/table.rb
3168
- # - inspect()
3669
+ # - table.inspect => string
3169
3670
  # -->
3170
- # Shows the mode and size of this table in a US-ASCII String.
3671
+ # Returns a `US-ASCII`-encoded String showing table:
3672
+ # * Class: `CSV::Table`.
3673
+ # * Access mode: `:row`, `:col`, or `:col_or_row`.
3674
+ # * Size: Row count, including the header row.
3675
+ #
3676
+ #
3677
+ # Example:
3678
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3679
+ # table = CSV.parse(source, headers: true)
3680
+ # table.inspect # => "#<CSV::Table mode:col_or_row row_count:4>\nName,Value\nfoo,0\nbar,1\nbaz,2\n"
3171
3681
  #
3172
3682
  def inspect: () -> String
3173
3683
 
@@ -3201,28 +3711,39 @@ class CSV::Table[out Elem] < Object
3201
3711
 
3202
3712
  # <!--
3203
3713
  # rdoc-file=lib/csv/table.rb
3204
- # - to_a()
3714
+ # - table.to_a -> array_of_arrays
3205
3715
  # -->
3206
- # Returns the table as an Array of Arrays. Headers will be the first row, then
3207
- # all of the field rows will follow.
3716
+ # Returns the table as an Array of Arrays; the headers are in the first row:
3717
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3718
+ # table = CSV.parse(source, headers: true)
3719
+ # table.to_a # => [["Name", "Value"], ["foo", "0"], ["bar", "1"], ["baz", "2"]]
3208
3720
  #
3209
3721
  def to_a: () -> untyped
3210
3722
 
3211
3723
  # <!--
3212
3724
  # rdoc-file=lib/csv/table.rb
3213
- # - to_csv(write_headers: true, **options)
3725
+ # - table.to_csv(**options) -> csv_string
3214
3726
  # -->
3215
- # Returns the table as a complete CSV String. Headers will be listed first, then
3216
- # all of the field rows.
3727
+ # Returns the table as CSV string. See [Options for
3728
+ # Generating](../CSV.html#class-CSV-label-Options+for+Generating).
3729
+ #
3730
+ # Defaults option `write_headers` to `true`:
3731
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3732
+ # table = CSV.parse(source, headers: true)
3733
+ # table.to_csv # => "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
3734
+ #
3735
+ # Omits the headers if option `write_headers` is given as `false` (see {Option
3736
+ # `write_headers`[}](../CSV.html#class-CSV-label-Option+write_headers)):
3737
+ # table.to_csv(write_headers: false) # => "foo,0\nbar,1\nbaz,2\n"
3217
3738
  #
3218
- # This method assumes you want the Table.headers(), unless you explicitly pass
3219
- # `:write_headers => false`.
3739
+ # Limit rows if option `limit` is given like `2`:
3740
+ # table.to_csv(limit: 2) # => "Name,Value\nfoo,0\nbar,1\n"
3220
3741
  #
3221
3742
  def to_csv: (?write_headers: boolish, **untyped) -> untyped
3222
3743
 
3223
3744
  # <!--
3224
3745
  # rdoc-file=lib/csv/table.rb
3225
- # - to_s(write_headers: true, **options)
3746
+ # - to_s(write_headers: true, limit: nil, **options)
3226
3747
  # -->
3227
3748
  #
3228
3749
  alias to_s to_csv