5 Replies - 3084 Views - Last Post: 31 May 2011 - 01:03 PM Rate Topic: -----

#1 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,209
  • Joined: 12-May 09

Another Ruby Exercise: CSV

Post icon  Posted 21 April 2011 - 06:09 PM

Ruby is an excellent language for quick and easy file processing. One way to practice ruby's file processing tools is to create a small csv parsing library.

For this exercise, try writing a script to parse and return information from a given csv script. There is no character limit or performance goal (although obtuse slowness is discouraged).

The script should support files with or without headers, and if the file has a header it should allow you to get values from each row by column name. The CSV library should be able to handle any CSV file.

For extra points (not that anybody's assigning points), make it possible to iterate over the rows in the file and the columns in a row.

This post has been edited by xclite: 05 May 2011 - 06:34 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Another Ruby Exercise: CSV

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Another Ruby Exercise: CSV

Posted 21 April 2011 - 06:22 PM

I wrote a blog post about this. :D

SPOILERS OF COURSE!
http://www.dreaminco...file-with-ruby/

Here's the code, first I created a Car class to hold values.

Spoiler


Then comes the magic:

Spoiler


Huzzah, god damn Ruby is sexy.

This post has been edited by Sergio Tapia: 21 April 2011 - 06:22 PM

Was This Post Helpful? 1
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,209
  • Joined: 12-May 09

Re: Another Ruby Exercise: CSV

Posted 22 April 2011 - 10:13 AM

Hm, I should have clarified that the csv library should be applicable to any sort of CSV, rather than a known format =p
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Another Ruby Exercise: CSV

Posted 22 April 2011 - 12:14 PM

But a CSV by definition is a collection of comma separated values.
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,209
  • Joined: 12-May 09

Re: Another Ruby Exercise: CSV

Posted 22 April 2011 - 06:10 PM

I mean that you don't know it's a car. Basically, I'm not saying use a CSV to populate an object, I'm saying create a useful CSV API.
Was This Post Helpful? 0
  • +
  • -

#6 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Another Ruby Exercise: CSV

Posted 31 May 2011 - 01:03 PM

Hey, snappy script, Sergio Tapia! Do you know how to get the fields into a mysql database with the proper datatypes (prescribed in the cars object definition of course).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1