# Csv help

**URL:** <https://www.ruby-forum.com/t/csv-help/98064>\
**Category:** Ruby\
**Created:** [January 27, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/csv-help/98064 "2006-01-27T22:17:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 27, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/csv-help/98064/1 "2006-01-27T22:17:54Z")

</div>

Can anyone point me to a good tutorial on csv files and ruby. I’ve read  
the docs that go along with the standard csv library but it’s hard to  
put it all together. I’m trying to write a simple timekeeper application  
using csv files but I don’t really know where to start. Maybe someone  
can point out the many flaws in this beginners attempt. Currently all  
I’m trying to do is create a row of time data each time the app is ran.

require ‘csv’  
class TimeKeeper

## constant of where the csv file is stored

@@csv\_file = “/shared/home/cmbowma/.timeclock”

def initialize  
end

def read  
@data = String.new  
CSV.parse open(@@csv\_file).read do |row|  
@data \<\< row.to\_s \<\< ‘,’  
end  
@data  
end

def write(data)  
writer = CSV.open(@@csv\_file, ‘w’)  
writer \<\< data  
writer.close  
end  
end

csv = TimeKeeper.new  
@data = csv.read.to\_a  
time = Time.now  
@data \<\< [time]  
csv.write(@data)

---

<div class="post-metadata">

**Author:** ![beachguy82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9dc877/32.png) [@beachguy82](https://www.ruby-forum.com/u/beachguy82)\
**Post date:** [January 27, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/csv-help/98064/2 "2006-01-27T22:26:45Z")

</div>

On Jan 27, 2006, at 3:18 PM, charlie bowman wrote:

> def read  
> @data = String.new  
> CSV.parse open(@@csv\_file).read do |row|  
> @data \<\< row.to\_s \<\< ‘,’  
> end  
> @data  
> end

To read a CSV file into an Array, try:

array\_of\_csv\_rows = CSV.read(“my\_file.csv”)

If you want to read CSV row by row, use:

CSV.foeach(“my\_file.csv”) do |row

# use row here

end

Hope that helps.

James Edward G. II
