Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

CSV::Table#[]=

table[n] = row → row
table[n] = field_or_array_of_fields → field_or_array_of_fields
table[header] = field_or_array_of_fields → field_or_array_of_fields

Puts data onto the table.


Set a Row by Its Integer Index
  • Form: table[n] = row, n an Integer, row a CSV::Row instance or an Array of fields.

  • Access mode: :row or :col_or_row.

  • Return value: row.

If the row exists, it is replaced:

Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
new_row = CSV::Row.new(['Name', 'Value'], ['bat', 3])
table.by_row! # => #<CSV::Table mode:row row_count:4>
return_value = table[0] = new_row
return_value.equal?(new_row) # => true # Returned the row
table[0].to_h # => {"Name"=>"bat", "Value"=>3}

With access mode :col_or_row:

Example
table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
table[0] = CSV::Row.new(['Name', 'Value'], ['bam', 4])
table[0].to_h # => {"Name"=>"bam", "Value"=>4}

With an Array instead of a CSV::Row, inherits headers from the table:

Example
array = ['bad', 5]
return_value = table[0] = array
return_value.equal?(array) # => true # Returned the array
table[0].to_h # => {"Name"=>"bad", "Value"=>5}

If the row does not exist, extends the table by adding rows: assigns rows with nil as needed:

Example
table.size # => 3
table[5] = ['bag', 6]
table.size # => 6
table[3] # => nil
table[4]# => nil
table[5].to_h # => {"Name"=>"bag", "Value"=>6}

Note that the nil rows are actually nil, not a row of nil fields.


Set a Column by Its Integer Index
  • Form: table[n] = array_of_fields, n an Integer, array_of_fields an Array of String fields.

  • Access mode: :col.

  • Return value: array_of_fields.

If the column exists, it is replaced:

Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
new_col = [3, 4, 5]
table.by_col! # => #<CSV::Table mode:col row_count:4>
return_value = table[1] = new_col
return_value.equal?(new_col) # => true # Returned the column
table[1] # => [3, 4, 5]
# The rows, as revised:
table.by_row! # => #<CSV::Table mode:row row_count:4>
table[0].to_h # => {"Name"=>"foo", "Value"=>3}
table[1].to_h # => {"Name"=>"bar", "Value"=>4}
table[2].to_h # => {"Name"=>"baz", "Value"=>5}
table.by_col! # => #<CSV::Table mode:col row_count:4>

If there are too few values, fills with nil values:

Example
table[1] = [0]
table[1] # => [0, nil, nil]

If there are too many values, ignores the extra values:

Example
table[1] = [0, 1, 2, 3, 4]
table[1] # => [0, 1, 2]

If a single value is given, replaces all fields in the column with that value:

Example
table[1] = 'bat'
table[1] # => ["bat", "bat", "bat"]

Set a Column by Its String Header
  • Form: table[header] = field_or_array_of_fields, header a String header, field_or_array_of_fields a field value or an Array of String fields.

  • Access mode: :col or :col_or_row.

  • Return value: field_or_array_of_fields.

If the column exists, it is replaced:

Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
new_col = [3, 4, 5]
table.by_col! # => #<CSV::Table mode:col row_count:4>
return_value = table['Value'] = new_col
return_value.equal?(new_col) # => true # Returned the column
table['Value'] # => [3, 4, 5]
# The rows, as revised:
table.by_row! # => #<CSV::Table mode:row row_count:4>
table[0].to_h # => {"Name"=>"foo", "Value"=>3}
table[1].to_h # => {"Name"=>"bar", "Value"=>4}
table[2].to_h # => {"Name"=>"baz", "Value"=>5}
table.by_col! # => #<CSV::Table mode:col row_count:4>

If there are too few values, fills with nil values:

Example
table['Value'] = [0]
table['Value'] # => [0, nil, nil]

If there are too many values, ignores the extra values:

Example
table['Value'] = [0, 1, 2, 3, 4]
table['Value'] # => [0, 1, 2]

If the column does not exist, extends the table by adding columns:

Example
table['Note'] = ['x', 'y', 'z']
table['Note'] # => ["x", "y", "z"]
# The rows, as revised:
table.by_row!
table[0].to_h # => {"Name"=>"foo", "Value"=>0, "Note"=>"x"}
table[1].to_h # => {"Name"=>"bar", "Value"=>1, "Note"=>"y"}
table[2].to_h # => {"Name"=>"baz", "Value"=>2, "Note"=>"z"}
table.by_col!

If a single value is given, replaces all fields in the column with that value:

Example
table['Value'] = 'bat'
table['Value'] # => ["bat", "bat", "bat"]
See also #[] #by_col #== #by_col! also in 3.3 · 3.2 · 3.0