Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@brianlamb
Last active December 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianlamb/7682256 to your computer and use it in GitHub Desktop.
Save brianlamb/7682256 to your computer and use it in GitHub Desktop.
Ruby 1.8.7 CSV example
#pre-fastercsv examples to import CSV data in Ruby 1.8.7
require 'csv'
CSV::Reader.parse(File.open('./db/sample_tasks.dat'), fs = 9) do |row|
next if row[0] == 'name'
Task.create!(
:name => row[0],
:description => row[1],
:duration => row[2],
:due_date => row[3],
:complete => row[4]
)
end
-------------
tasks = CSV.open('./db/sample_tasks.dat', 'r', fs = 9)
=> #<CSV::IOReader:0x7f8b0eddc200 @dev=#<CSV::IOBuf:0x7f8b0eddc1b0 @cur_buf=0, @buf_tail_idx=0, @is_eos=false, @buf_list
=["name\tdescription\tduration\tdue date\tcomplete\nWrite paper\tHenry Knox and the Siege of Boston\t240\t2014-Nov-06\tt
rue\nBuy turkey\tDon't forget the cranberries.\t60\t2014-Nov-27\tfalse\nLibrary Study\tCheck that the wifi's working\t90
\t2014-Nov-06\ttrue\nMovie night\tChinatown\t140\t2014-Nov-12\tfalse"], @s=#<File:./db/sample_tasks.dat>, @offset=0>, @i
dx=0, @close_on_terminate=true, @rs=nil, @fs=9, @io=#<File:./db/sample_tasks.dat>>
irb(main):003:0> tasks = CSV.open('./db/sample_tasks.dat', 'r', fs = 9) do |row|
--------------
CSV.open('./db/sample_tasks.dat', 'r', fs = 9) do |task|
puts task[0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment