This is a RubyMotion friendly port of fasterer-csv by Mason: http://rubygems.org/gems/fasterer-csv
Add this line to your application's Gemfile:
gem 'motion-csv'
And then execute:
$ bundle
Or install it yourself as:
$ gem install motion-csv
Check out the specs
directory for usage examples, but here's a few brief examples:
csv_string = "a,b,c,d\n1,2,3,4\n5,6,7,whatever\n"
csv = MotionCSV.parse(csv_string)
puts csv.headers # [:a, :b, :c, :d]
puts csv.first[:b] # 2
puts csv.last[:d] # "whatever"
MotionCSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
end
# "row,of,CSV,data\nanother,row\n"
This uses a convenience method on the Array
class. You can pass it a single or two-dimensional array.
["testing", "arrays"].to_csv
# "testing,arrays\n"
[
['array1', 'stuff'],
['array2', 'more stuff']
].to_csv
# "array1,stuff\narray2,more stuff\n"
This uses a convenience method on the String
class.
"header1,header2\nCSV,String".parse_csv
# [["CSV", "String"]]
To run the testing suite, run rake spec
.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request