Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Extension to Rubys CSV class that lets you iterate over specific column values with dynamic methods
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README
csv_by_column.rb

README

iterate CSV data by colum easily

Created by Stephan Ziep on 2011-04-28.
Copyright 2011 Stephan Ziep. You can redistribute or modify this code
under the terms of Ruby's license.

See CSV for documentation.

== Description
This small addition to Rubys default CSV class, enhances it in a way
that lets you iterate over columns in a nice way syntacticly
E.g: given a csv dataset like:

Name,Nickname,Email,Message,ID
Stephan,nick1,stephan@mail.de,"A first message",3
Nico,nick2,nico@mail.de,"A second message",6
Richard,nick3,richard@mail.de,"A third message",67
Marc,nick4,marc@mail.de,"A fourth message",1234

You can directly refer to the header (downcased) in the foreach method
like this:

OPTIONS = { converters:         :all,
            headers:            true,
            header_converters:  :downcase}

CSV.foreach_message("data.csv", OPTIONS).each do |msg|
  p msg
end

CSV.foreach_message_and_nickname("data.csv", OPTIONS).each do |msg|
  p msg
end

or you use the foreach_column method directly: 

CSV.foreach_column("data.csv", OPTIONS, "email") do |mail|
  p mail
end

Something went wrong with that request. Please try again.