Get In That DB! Parsing CSV Using Ruby...

Posted by keruilin on Stack Overflow See other posts from Stack Overflow or by keruilin
Published on 2010-04-09T18:28:25Z Indexed on 2010/04/09 21:23 UTC
Read the original article Hit count: 386

Filed under:
|
|
|

I have a CSV file formatted just like this:

name,color,tasty,qty
apple,red,true,3
orange,orange,false,4
pear,greenish-yellowish,true,1

As you can see, each column in the Ruby OO world represents a mix of types -- string, string, boolean, int.

Now, ultimately, I want to parse each line in the file, determine the appropriate type, and insert that row into a database via a Rails migration. For ex:

Fruit.create(:name => 'apple', :color => 'red', :tasty => true, :qty => 3)

Help!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about csv