Subscribe to Sergio Tapia - Lost in the GC.        RSS Feed
-----

Let's parse a CSV file with Ruby.

Icon 2 Comments
Last night was my first time actually reading and learning about Ruby. I got the famous Pickaxe book and started things out. I'm really surprised at how easy the language makes things for me as a developer. It just lets me do things, without getting in the way. Definitely a breath of fresh air after working for more than 3 years exclusively with C#.

From here on out all of my Ruby posts, I'll be using Ruby 1.8 (got this using the ruby -v terminal command) and Linux Mint.

This is what we're going to parse:

Posted Image

Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
2003,Saturn,Ion,2.35




First, let's create a Car class, think of this as a simple data holder object.

class Car
  attr_accessor :year, :make, :model, :length
  
  def initialize(year, make, model, length)
    @year = year
    @make = make
    @model = model
    @length = length
  end
  
  def to_s
    "Year: #{@year} | Make: #{@make} | Model: #{@model} | Length: #{@length}"
  end
end


The 'to_s' function allows us to sculpt what we want to output to the terminal when we call a Car object via the 'puts' command. If you're coming from a C# background, this is similar to overriding the ToString() method when you're trying to debug something.

Next, let's read the information from the CSV file and load our car objects.

#This is similar to C#'s using statements.
require 'csv'

cars = Array.new
CSV.foreach('/home/stapiagutierrez/Desktop/Pickaxe/cars.csv') do |row|
  cars << Car.new(*row)
end



We're telling Ruby to get each row in the CSV file, assign it to the temporary variable 'row' and then using a splat operator we assign it to the car object.

What, were you expecting more? Nope, this is all you need. I'm a firm believer that less code = less bugs; so Ruby seems to fit in very well with me.

Let's display our fetched cars, shall we?

cars.each do |car|
  puts car  
end


Haha! Doesn't that code just make you giddy? :)

Here's the entire code block for you to check out:

class Car
  attr_accessor :year, :make, :model, :length
  
  def initialize(year, make, model, length)
    @year = year
    @make = make
    @model = model
    @length = length
  end
  
  def to_s
    "Year: #{@year} | Make: #{@make} | Model: #{@model} | Length: #{@length}"
  end
end

require 'csv'

cars = Array.new
CSV.foreach('/home/stapiagutierrez/Desktop/Pickaxe/cars.csv') do |row|
  cars << Car.new(*row)
end

cars.each do |car|
  puts car  
end



I'll be posting more and more Ruby related things as I come across them, so hopefully we can learn together. If you're an intermediate or advanced Ruby programmer please let me know if there is a more ruby-esque way to do things, I'm eager to learn! :D

2 Comments On This Entry

Page 1 of 1

Curtis Rutland 

16 February 2011 - 07:39 PM
I might have to get into ruby. More specifically, RoR. I've been meaning to set up a virtual Linux instance on my server. One day I'll have to stop being lazy and do it. Of course, I'm learning MVC3 right now, so it might have to wait.

Are you working through a series of tutorials or a book that you'd suggest?
0

Sergio Tapia 

17 February 2011 - 04:40 AM
I'm reading through the Pickaxe book linked up top. It assumes no previous knowledge on Ruby so it's great for a beginner.

After I'm done with this I'll read Head First Rails.
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)

About Me

Posted Image


Bienvenidos! I'm a USA ex-pat living in Bolivia for the past 10 years. Web development is my forte with a heavy lean for usability and optimization. I'm fluent in both English and Spanish. I guest write for the popular Python website Python Central. Visit my website.

Categories