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:

First, let's create a Car class, think of this as a simple data holder object.
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.
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?
Haha! Doesn't that code just make you giddy? :)
Here's the entire code block for you to check out:
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
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:

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?
Are you working through a series of tutorials or a book that you'd suggest?
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
About Me

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.
My Blog Links
Recent Entries
-
-
-
DevOps for dummies - VPS Configuration from scratch - Rails, Nginx, PostgreSQL.
on May 22 2013 02:36 PM
-
-
Recent Comments
-
Sergio Tapia on May 24 2013 12:33 PM
DevOps for dummies - VPS Configuration from scratch - Rails, Nginx, PostgreSQL.
-
Lemur on May 23 2013 04:53 PM
DevOps for dummies - VPS Configuration from scratch - Rails, Nginx, PostgreSQL.
-
laytonsdad on Apr 30 2013 11:30 AM
Dream.In.Code Badge Generator! Share your flair on your site or blog.
-
-
Jstall on Nov 04 2012 09:18 AM
The Pragmatic Bookshelf mega blowout sale - 40% off select Ruby on Rails books.



2 Comments








|