Skip to content

Commit

Permalink
csv use header
Browse files Browse the repository at this point in the history
  • Loading branch information
halida committed Sep 26, 2014
1 parent 148a79f commit 49f768e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ruby/csv/run.rb
Expand Up @@ -5,11 +5,24 @@

# write
CSV.open(filename, "wb+") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
csv << ["ID", "name", "value"]
csv << [1, "halida", 120]
csv << [2, "James", 300]
end

# read as table
c = CSV.new(File.open(filename), headers: :first_row)
# read first
row = c.readline
# read all
c.read

puts "ID: #{row['ID']}"
# return all fields
puts "first line: #{row.fields}"

# read all
CSV.foreach(filename) do |row|
puts row.to_s
row.to_s
end

0 comments on commit 49f768e

Please sign in to comment.