End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
CSV::Table#[]=
# File tmp/rubies/ruby-3.1.3/lib/csv/table.rb, line 649
def []=(index_or_header, value)
if @mode == :row or # by index
(@mode == :col_or_row and index_or_header.is_a? Integer)
if value.is_a? Array
@table[index_or_header] = Row.new(headers, value)
else
@table[index_or_header] = value
end
else # set column
unless index_or_header.is_a? Integer
index = @headers.index(index_or_header) || @headers.size
@headers[index] = index_or_header
end
if value.is_a? Array # multiple values
@table.each_with_index do |row, i|
if row.header_row?
row[index_or_header] = index_or_header
else
row[index_or_header] = value[i]
end
end
else # repeated value
@table.each do |row|
if row.header_row?
row[index_or_header] = index_or_header
else
row[index_or_header] = value
end
end
end
end
end
Puts data onto the table.
Seta Row by Its Integer Index
-
Form:
table[n] = row,nan Integer,rowa CSV::Row instance or an Array of fields. -
Access mode:
:rowor:col_or_row. -
Return value:
row.
If the row exists, it is replaced:
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:
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:
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:
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.
Seta Column by Its Integer Index
-
Form:
table[n] = array_of_fields,nan Integer,array_of_fieldsan Array of String fields. -
Access mode:
:col. -
Return value:
array_of_fields.
If the column exists, it is replaced:
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:
table[1] = [0] table[1] # => [0, nil, nil]
If there are too many values, ignores the extra values:
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:
table[1] = 'bat' table[1] # => ["bat", "bat", "bat"]
Seta Column by Its String Header
-
Form:
table[header] = field_or_array_of_fields,headera String header,field_or_array_of_fieldsa field value or an Array of String fields. -
Access mode:
:color:col_or_row. -
Return value:
field_or_array_of_fields.
If the column exists, it is replaced:
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:
table['Value'] = [0] table['Value'] # => [0, nil, nil]
If there are too many values, ignores the extra values:
table['Value'] = [0, 1, 2, 3, 4] table['Value'] # => [0, 1, 2]
If the column does not exist, extends the table by adding columns:
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:
table['Value'] = 'bat' table['Value'] # => ["bat", "bat", "bat"]