Ruby csv: Difference between revisions

From wikinotes
No edit summary
No edit summary
 
Line 5: Line 5:




csv = CSV.new("id, name\n123, foouser\n345, baruser", headers: true)
csv = CSV.new(<<~CSV, headers: true)
id, name
123, foouser
456, baruser
CSV
# each time you call `csv.first`, it returns the next row
# each time you call `csv.first`, it returns the next row
csv.first # {id: 123, name: "foouser" }
csv.first # {id: 123, name: "foouser" }
csv.first # {id: 456, name: "baruser" }
csv.first # {id: 456, name: "baruser" }
</source>
</source>

Latest revision as of 18:27, 7 December 2023

csv = CSV.new(File.read('out.csv'))
csv.read          # array of rows
csv << [0, 1, 2]  # append row to csv


csv = CSV.new(<<~CSV, headers: true)
id, name
123, foouser
456, baruser
CSV
# each time you call `csv.first`, it returns the next row
csv.first # {id: 123, name: "foouser" }
csv.first # {id: 456, name: "baruser" }