Project

csvp

0.0
No commit activity in last 3 years
No release in over 3 years
Effortlessly print enumerables as CSV.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.1
~> 0.10
~> 3.4
~> 1.3
 Project Readme

csvp

Build Status codecov

csvp helps you effortlessly print enumerables as CSV.

Inspired by awesome_print and useful for getting your data from Ruby console to Excel or Slack.

Give it an Enumerable of objects and they will be printed as CSV on best effort basis. It will even determine the header if possible. Within your enumerable, you can not only use standard Ruby objects like Array, Hash, Struct or OpenStruct but also ActiveRecord objects from Rails.

Installation

$ gem install csvp

Usage

First, require csvp in your program

> require 'csvp'

Then print an Array of Array-s

> csvp [['Alice', 'Bob'],['Chuck', 'Donald']]
Alice,Bob
Chuck,Donald

or an Array of Hash-es

> csvp [{'Alice': 1, 'Bob': 2}, {'Alice': 3, 'Bob': 4}]
Alice,Bob
1,2
3,4

or some ActiveRecord objects

> csvp User.all
email,name,age
alice@csvp.com,Alice,29
bob@csvp.com,Bob,43

Options

All of the options supported by Ruby CSV module can be used.

So you can choose the column separator

> csvp [['Alice', 'Bob'],['Chuck', 'Donald']], col_sep: "\t"
Alice	Bob
Chuck	Donald

or the quote character

> csvp [['Alice', 'Bob'],['Chuck', 'Donald']], quote_char: "\"", force_quotes: true
"Alice","Bob"
"Chuck","Donald"

Conveniently, the two options above are wrapped in their own method - tsvp and qcsvp respectively.

Other CSV goodies

  • q is a command line tool that allows direct execution of SQL-like queries on CSVs/TSVs

  • conformist allows to bend CSVs to your will with declarative schemas