martin carpenter

csvobj

api documentation http://mcarpenter.org/rdoc/csvobj/index.html
github home http://github.com/mcarpenter/csvobj
repository URLs https://github.com/mcarpenter/csvobj.git
git://github.com/mcarpenter/csvobj.git
page last updated 2010-11-29 22:57 +01:00

aims

CSVobj has two simple aims in the processing of CSV files:

It achieves these aims by automatically instantiating a class with dynamic attributes that can be used to refer to each column of the CSV by name. Each attribute name is derived in a predictable fashion from the column headers (the first row of the CSV).

clarity

Rather than referring programatically to the "third element of the row" (row[2]) one can simply say row.last_name.

robustness

Any change to the CSV format (eg by inserting a new column between two existing columns) will not require a program change: you do not need to hunt down all references to row[2] and make them row[3]; they remain row.last_name.

example

Given the following CSV file:

    First Name,Last Name
    Cheryl,James
    Sandra,Denton
    Deidra,Roper

we can extract just the last names as follows:

    # Get the gem
    require 'rubygems'
    require 'csvobj'

    # Subclass because #parse defines methods on the class
    class MyCsv < CSVobj ; end

    # First command line argument is CSV file to read
    csv_file = File.new( ARGV[0] )

    # Parse the file, print the "last name" column of each row
    MyCsv.parse(csv_file).each do |row|
      puts row.last_name
    end