Skip to content

jeremyevans/enum_csv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnumCSV exposes a single method, csv, for easily creating CSV output/files from enumerable objects. It is a simple wrapper class for ruby’s csv library.

gem install enum_csv

Source code is available on GitHub at github.com/jeremyevans/enum_csv

The default behavior just expects an enumerable of arrays (such as an array of arrays), and returns a string containing the CSV output:

EnumCSV.csv([[1, 2]]) => "1,2\n"

You can use the :headers option to set custom headers:

EnumCSV.csv([[1, 2], [3,"4,5"]], :headers=>['A', 'B']) => "A,B\n1,2\n3,\"4,5\"\n"
EnumCSV.csv([[1, 2], [3,"4,5"]], :headers=>'A,B') => "A,B\n1,2\n3,\"4,5\"\n"

The :file option can be used to output to a file:

EnumCSV.csv([[1, 2]], :file=>'foo.csv') => nil # output written to foo.csv

If a block is passed to the method, all items in the enumerable are yielded to the block, and the block should return an array with the data to use in the CSV output:

EnumCSV.csv([{:a=>1, :b=>2}, {:a=>3, :b=>4}]){|l| [l[:b], l[:a] + 10]} => "2,11\n4,13\n"

MIT

Jeremy Evans <code@jeremyevans.net>

About

Create CSV from Enumerables

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages