Skip to content
Extension to Rubys CSV class that lets you iterate over specific column values with dynamic methods
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


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:

Stephan,nick1,,"A first message",3
Nico,nick2,,"A second message",6
Richard,nick3,,"A third message",67
Marc,nick4,,"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

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

or you use the foreach_column method directly: 

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

Something went wrong with that request. Please try again.