Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
CSV::Row.new
CSV::Row.new(headers, fields, header_row = false) → csv_row
lib/csv/row.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/csv/row.rb, line 105
def initialize(headers, fields, header_row = false)
@header_row = header_row
headers.each { |h| h.freeze if h.is_a? String }
# handle extra headers or fields
@row = if headers.size >= fields.size
headers.zip(fields)
else
fields.zip(headers).each(&:reverse!)
end
end
Returns the new CSV::Row instance constructed from arguments headers and fields; both should be Arrays; note that the fields need not be Strings:
Example
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:
Example
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:
Example
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:
Example
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