class CSV::Row
CSV::Row
A CSV::Row instance represents a CSV table row. (see class CSV).
The instance may have: * Fields: each is an object, not necessarily a String. * Headers: each serves a key, and also need not be a String.
Instance Methods
CSV::Row has three groups of instance methods: * Its own internally defined instance methods. * Methods included by module Enumerable. * Methods delegated to class Array.: * Array#empty? * Array#length * Array#size
Creating a CSV::Row Instance
Commonly, a new CSV::Row instance is created by parsing CSV source that has headers: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) table.each {|row| p row }
Output: #<CSV::Row “Name”:“foo” “Value”:“0”> #<CSV::Row “Name”:“bar” “Value”:“1”> #<CSV::Row “Name”:“baz” “Value”:“2”>
You can also create a row directly. See ::new.
Headers
Like a CSV::Table, a CSV::Row has headers.
A CSV::Row that was created by parsing CSV source inherits its headers from the table: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table.first row.headers # => [“Name”, “Value”]
You can also create a new row with headers; like the keys in a Hash, the headers need not be Strings: row = CSV::Row.new([:name, :value], [‘foo’, 0]) row.headers # => [:name, :value]
The new row retains its headers even if added to a table that has headers: table << row # => #<CSV::Table mode:col_or_row row_count:5> row.headers # => [:name, :value] row # => “foo” row # => nil
Accessing Fields
You may access a field in a CSV::Row with either its Integer index (Array-style) or its header (Hash-style).
Fetch a field using method []: row = CSV::Row.new([‘Name’, ‘Value’], [‘foo’, 0]) row # => 0 row # => 0
Set a field using method []=: row = CSV::Row.new([‘Name’, ‘Value’], [‘foo’, 0]) row # => #<CSV::Row “Name”:“foo” “Value”:0> row = ‘bar’ row = 1 row # => #<CSV::Row “Name”:“bar” “Value”:1>
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2286
def initialize: (Array[untyped] headers, Array[untyped] fields, ?header_row: bool) -> void
Returns the new CSV::Row instance constructed from arguments headers and fields; both should be Arrays; note that the fields need not be Strings: row = CSV::Row.new([‘Name’, ‘Value’], [‘foo’, 0]) row # => #<CSV::Row “Name”:“foo” “Value”:0>
If the Array lengths are different, the shorter is nil-filled: row = CSV::Row.new([‘Name’, ‘Value’, ‘Date’, ‘Size’], [‘foo’, 0]) row # => #<CSV::Row “Name”:“foo” “Value”:0 “Date”:nil “Size”:nil>
Each CSV::Row object is either a field row or a header row; by default, a new row is a field row; for the row created above: row.field_row? # => true row.header_row? # => false
If the optional argument header_row is given as true, the created row is a header row: row = CSV::Row.new([‘Name’, ‘Value’], [‘foo’, 0], header_row = true) row # => #<CSV::Row “Name”:“foo” “Value”:0> row.field_row? # => false row.header_row? # => true
Public Instance Methods
(untyped arg) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2319
def <<: (untyped arg) -> untyped
Adds a field to self; returns self:
If the argument is a 2-element Array [header, value], a field is added with the given header and value: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row << [‘NAME’, ‘Bat’] row # => #<CSV::Row “Name”:“Foo” “Name”:“Bar” “Name”:“Baz” “NAME”:“Bat”>
If the argument is a Hash, each key-value pair is added as a field with header key and value value. source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row << {NAME: ‘Bat’, name: ‘Bam’} row # => #<CSV::Row “Name”:“Foo” “Name”:“Bar” “Name”:“Baz” NAME:“Bat” name:“Bam”>
Otherwise, the given value is added as a field with no header. source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row << ‘Bag’ row # => #<CSV::Row “Name”:“Foo” “Name”:“Bar” “Name”:“Baz” nil:“Bag”>
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2335
def ==: (untyped other) -> bool
Returns true if other is a /CSV::Row that has the same fields (headers and values) in the same order as self; otherwise returns false: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table other_row = table row == other_row # => true other_row = table row == other_row # => false
(*untyped args) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2396
def []=: (*untyped args) -> untyped
Assigns the field value for the given index or header; returns value.
Assign field value by Integer index: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table row = ‘Bat’ row = 3 row # => #<CSV::Row “Name”:“Bat” “Value”:3>
Counts backward from the last column if index is negative: row = 4 row = ‘Bam’ row # => #<CSV::Row “Name”:“Bam” “Value”:4>
Extends the row with nil:nil if positive index is not in the row: row = 5 row # => #<CSV::Row “Name”:“bad” “Value”:4 nil:nil nil:nil nil:5>
Raises IndexError if negative index is too small (too far from zero).
Assign field value by header (first found): source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row = ‘Bat’ row # => #<CSV::Row “Name”:“Bat” “Name”:“Bar” “Name”:“Baz”>
Assign field value by header, ignoring offset leading fields: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row[‘Name’, 2] = 4 row # => #<CSV::Row “Name”:“Foo” “Name”:“Bar” “Name”:4>
Append new field by (new) header: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table row = 6 row# => #<CSV::Row “Name”:“foo” “Value”:“0” “New”:6>
(untyped header_or_index, ?untyped minimum_index) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2433
def delete: (untyped header_or_index, ?untyped minimum_index) -> untyped
Removes a specified field from self; returns the 2-element Array [header, value] if the field exists.
If an Integer argument index is given, removes and returns the field at offset index, or returns nil if the field does not exist: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.delete(1) # => [“Name”, “Bar”] row.delete(50) # => nil
Otherwise, if the single argument header is given, removes and returns the first-found field with the given header, of returns a new empty Array if the field does not exist: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.delete(‘Name’) # => [“Name”, “Foo”] row.delete(‘NAME’) # => []
If argument header and Integer argument offset are given, removes and returns the first-found field with the given header whose index is at least as large as offset: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.delete(‘Name’, 1) # => [“Name”, “Bar”] row.delete(‘NAME’, 1) # => []
() { (*untyped) → untyped } → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2452
def delete_if: () { (*untyped) -> untyped } -> untyped
Removes fields from self as selected by the block; returns self.
Removes each field for which the block returns a truthy value: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.delete_if {|header, value| value.start_with?(‘B’) } # => true row # => #<CSV::Row “Name”:“Foo”> row.delete_if {|header, value| header.start_with?(‘B’) } # => false
If no block is given, returns a new Enumerator: row.delete_if # => #<Enumerator: #<CSV::Row “Name”:“Foo”>:delete_if>
(untyped index_or_header, *untyped indexes) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2472
def dig: (untyped index_or_header, *untyped indexes) -> untyped
Finds and returns the object in nested object that is specified by index_or_header and specifiers.
The nested objects may be instances of various classes. See Dig Methods.
Examples: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table row.dig(1) # => “0” row.dig(‘Value’) # => “0” row.dig(5) # => nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2492
def each: () -> Enumerator[Array[String], self]
| () { (Array[String]) -> void } -> self
Calls the block with each header-value pair; returns self: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.each {|header, value| p [header, value] }
Output: [“Name”, “Foo”] [“Name”, “Bar”] [“Name”, “Baz”]
If no block is given, returns a new Enumerator: row.each # => #<Enumerator: #<CSV::Row “Name”:“Foo” “Name”:“Bar” “Name”:“Baz”>:each>
(*untyped args) { (*untyped) → untyped } → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2502
def empty?: (*untyped args) { (*untyped) -> untyped } -> bool
(untyped header, *untyped varargs) ?{ (*untyped) → untyped } → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2544
def fetch: (untyped header, *untyped varargs) ?{ (*untyped) -> untyped } -> untyped
Returns the field value as specified by header.
With the single argument header, returns the field value for that header (first found): source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.fetch(‘Name’) # => “Foo”
Raises exception KeyError if the header does not exist.
With arguments header and default given, returns the field value for the header (first found) if the header exists, otherwise returns default: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.fetch(‘Name’, ”) # => “Foo” row.fetch(:nosuch, ”) # => “”
With argument header and a block given, returns the field value for the header (first found) if the header exists; otherwise calls the block and returns its return value: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.fetch(‘Name’) {|header| fail ‘Cannot happen’ } # => “Foo” row.fetch(:nosuch) {|header| “Header ‘#{header} not found’” } # => “Header ‘nosuch not found’”
(untyped header_or_index, ?untyped minimum_index) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2587
def field: (untyped header_or_index, ?untyped minimum_index) -> untyped
Returns the field value for the given index or header.
Fetch field value by Integer index: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table row.field(0) # => “foo” row.field(1) # => “bar”
Counts backward from the last column if index is negative: row.field(-1) # => “0” row.field(-2) # => “foo”
Returns nil if index is out of range: row.field(2) # => nil row.field(-3) # => nil
Fetch field value by header (first found): source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.field(‘Name’) # => “Foo”
Fetch field value by header, ignoring offset leading fields: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.field(‘Name’, 2) # => “Baz”
Returns nil if the header does not exist.
(untyped data) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2600
def field?: (untyped data) -> bool
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2608
def field_row?: () -> bool
Returns true if this is a field row, false otherwise.
(*untyped headers_and_or_indices) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2654
def fields: (*untyped headers_and_or_indices) -> untyped
Returns field values per the given specifiers, which may be any mixture of: * Integer index. * Range of Integer indexes. * 2-element Array containing a header and offset. * Header. * Range of headers.
For specifier in one of the first four cases above, returns the result of self.field(specifier); see field.
Although there may be any number of specifiers, the examples here will illustrate one at a time.
When the specifier is an Integer index, returns self.field(index)L source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.fields(1) # => [“Bar”]
When the specifier is a Range of Integers range, returns self.field(range): row.fields(1..2) # => [“Bar”, “Baz”]
When the specifier is a 2-element Array array, returns self.field(array)L row.fields(‘Name’, 1) # => [“Foo”, “Bar”]
When the specifier is a header header, returns self.field(header)L row.fields(‘Name’) # => [“Foo”]
When the specifier is a Range of headers range, forms a new Range new_range from the indexes of range.start and range.end, and returns self.field(new_range): source = “Name,NAME,name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.fields(‘Name’..‘NAME’) # => [“Foo”, “Bar”]
Returns all fields if no argument given: row.fields # => [“Foo”, “Bar”, “Baz”]
(untyped header) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2662
def has_key?: (untyped header) -> bool
Returns true if there is a field with the given header, false otherwise.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2677
def header_row?: () -> bool
Returns true if this is a header row, false otherwise.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2689
def headers: () -> untyped
Returns the headers for this row: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table.first row.headers # => [“Name”, “Value”]
(untyped header, ?untyped minimum_index) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2718
def index: (untyped header, ?untyped minimum_index) -> untyped
Returns the index for the given header, if it exists; otherwise returns nil.
With the single argument header, returns the index of the first-found field with the given header: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.index(‘Name’) # => 0 row.index(‘NAME’) # => nil
With arguments header and offset, returns the index of the first-found field with given header, but ignoring the first offset fields: row.index(‘Name’, 1) # => 1 row.index(‘Name’, 3) # => nil
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2733
def inspect: () -> String
(*untyped args) { (*untyped) → untyped } → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2742
def length: (*untyped args) { (*untyped) -> untyped } -> untyped
(*untyped args) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2762
def push: (*untyped args) -> untyped
(*untyped args) { (*untyped) → untyped } → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2764
def size: (*untyped args) { (*untyped) -> untyped } -> untyped
(**untyped) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2776
def to_csv: (**untyped) -> untyped
Returns the row as a CSV String. Headers are not included: source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table row.to_csv # => “foo,0\n”
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/csv/0/csv.rbs, line 2795
def to_h: () -> untyped
Returns the new Hash formed by adding each header-value pair in self as a key-value pair in the Hash. source = “Name,Value\nfoo,0\nbar,1\nbaz,2\n” table = CSV.parse(source, headers: true) row = table row.to_h # => {“Name”=>“foo”, “Value”=>“0”}
Header order is preserved, but repeated headers are ignored: source = “Name,Name,Name\nFoo,Bar,Baz\n” table = CSV.parse(source, headers: true) row = table row.to_h # => {“Name”=>“Foo”}