End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
CSV#<<
csv << row → self
lib/csv.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/lib/csv.rb, line 2371
def <<(row)
writer << row
self
end
Appends a row to self.
-
Argument
rowmust be an Array object or aCSV::Rowobject. -
The output stream must be open for writing.
Append Arrays:
Example
CSV.generate do |csv| csv << ['foo', 0] csv << ['bar', 1] csv << ['baz', 2] end # => "foo,0\nbar,1\nbaz,2\n"
Append CSV::Rows:
Example
headers = [] CSV.generate do |csv| csv << CSV::Row.new(headers, ['foo', 0]) csv << CSV::Row.new(headers, ['bar', 1]) csv << CSV::Row.new(headers, ['baz', 2]) end # => "foo,0\nbar,1\nbaz,2\n"
Headers in CSV::Row objects are not appended:
Example
headers = ['Name', 'Count'] CSV.generate do |csv| csv << CSV::Row.new(headers, ['foo', 0]) csv << CSV::Row.new(headers, ['bar', 1]) csv << CSV::Row.new(headers, ['baz', 2]) end # => "foo,0\nbar,1\nbaz,2\n"
Raises an exception if row is not an Array or CSV::Row:
Example
CSV.generate do |csv| # Raises NoMethodError (undefined method `collect' for :foo:Symbol) csv << :foo end
Raises an exception if the output stream is not opened for writing:
Example
path = 't.csv' File.write(path, '') File.open(path) do |file| CSV.open(file) do |csv| # Raises IOError (not opened for writing) csv << ['foo', 0] end end