Csv help

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)

···

--
Posted via http://www.ruby-forum.com/.

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 Gray II

···

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