Monthly Archives: October 2012

What I learned today: The for iterator

I was playing around with some practice exercises in Ruby today when I came across this sample code:ruby rails

This defines a method called parks_per_city which takes in an array of hashes as an argument and maps a city to the number of parks located within that city. Example :

ruby

This is done by looking at the value associated with the us_city key – which will be some city, supplying that value to the city hash as a key, and setting the value equal to 1 if that city doesn’t already exist, or adding to the value ( the number of parks ) if a city already exists as a key within the hash.
What I had not seen before is the line before the do..end block:

for park_city in parks

Even though it becomes obvious what this code does by reading the 11 lines of code above, I decided to google it to understand HOW this works. After researching different resources online, I found myself asking “Is it a loop method? Is it an iterator method? Is it both?”. According to this great blog called Snorks, it is a loop method that acts like an iterator without actually having to take a block. Here’s an example taken directly from the Snorks blog post:

ruby

However, when looking at the first screen-shot above, we see that the for loop is passed a do..end block. Then I read Chris Pines’ “Learn to Program” book – which I highly recommend if you’re a beginner like myself trying to learn Ruby and Rails. In chapter 7, he mentions that iterators are always followed by blocks such as do..end or {...}. He also goes on to mention that these blocks are only used with iterators. Hmm… So which is it, an iterator or a loop? I’m sure this probably won’t bother most people, but not being able to figure this out is bugging the hell out of me. So to better understand how this code works and hoping it would provide some insight on whether it’s a loop or an iterator, I decided to re-write the code using an iterator method I’m already familiar with and testing the parks_per_city method to see if it works. I used the each iterator. Here’s the code I wrote: ruby

As you can see, by replacing: for park_city in parks do... with parks.each do |park_city|... I was able to accomplish the same task. This each iterator method is pretty straight forward. It grabs each hash in the parks array and assigns it to the park_city variable as a hash. It then checks to see if the us_city in question is supplied to the city hash as a key. If not, then it’ll include the key to the hash and set the value equal to 1. If the city hash already has the us_city as a key then, it will add to the number of parks in that city.

I therefore can’t help but to conclude that:
for value in iterable do
...
end

in this context is an iterator method that executes whatever is in the block; just like:
iterable.each do |value|
...
end
.

Am I correct in my conclusion? What are your thoughts? Leave a comment below and let me know what you think!

Tagged , , , , ,

Ruby on Rails Tutorial: Validations

I’m currently going through the Ruby on Rails Tutorial by Michael Hartl and learned something important about the uniqueness validation argument on the validates method. First of all, the validates method is provided by Active Record and allows you to impose constraints on model attributes. For example, let’s say you have a User model and one of it’s attributes is a user’s email address. Well the uniqueness validation argument enforces that every email submitted to the database must be unique. It’s obvious why this is an important constraint to enforce when creating a web app. But there’s a problem; If a user mistakenly submits a sign-up form twice, Rails will still create two different users with the same attributes despite the uniqueness validation. What I learned is that in order to solve this you must also enforce uniqueness at the database level by creating a database index on the email column and require that the index be unique.  Going through the tutorial, I first generated a migration to add structure to the existing User model:

I then opened up the migration file and included the add_index method to add an index on the email column of the users table and enforced uniqueness:

Don’t forget to run the migration when you’re done:

 

 

There’s still one more step; we have to make sure that the email attribute gets saved into the database as all lowercase in order for this to work. This is because not all database adapters use case-sensitive indices. So the way to do this is by writing the before_save callback method in the User model and pass a block that sets the user’s email to all downcase using the downcase string method:

 

Now we can rest assured that the uniqueness validation will enforce that each user has a unique email address!

Tagged , , , ,

What I learned today: REST

Rails implements the REST architecture when creating web apps. This means that many components of a Rails application (ie. blog posts and/or users) are modeled as resources that follow CRUD operations that correspond to the four fundamental HTTP request methods. CRUD stands for Create, Read, Update, and Destroy. These are the major functions that are implemented in relational database applications that allow for resources (such as blog posts) to be, well, created, read, updated, and deleted. Now the four fundamental HTTP request methods are GET, POST, PUT, and DELETE. These request methods, or verbs, indicate the desired operation (CRUD) to be performed on resources. Here’s a table illustrating these operations and their corresponding HTTP requests.

REST

 

Tagged , , , ,

Working hard to become a Rails developer!

Hi! I’m Danny.

I come from a background in finance and as an undergrad I always enjoyed learning about our world economies and how our financial markets worked. But after graduating and beginning a career as a futures broker, I quickly realized that when selling really risky stuff it’s kinda hard to help and add value in peoples lives. That’s when I learned about web development and all the awesome educational resources online; long story short, I’ve finally found something that I’m truly passionate about. It’s hard to describe how exhilarating it is to learn something new and be able to apply what you’ve learned immediately; I’m getting high off of coding!

I’m currently going through Michael Hartl’s Ruby on Rails Tutorial  and building a twitter-like web app. I’m also going through a few tutorials provided by Treehouse and Code school. If you’re looking for great resources to learn web development and more, check these out!

The main reason for this blog is to gather my thoughts on things I’m learning along the way to aid me in my learning process. My goal is to learn how to build things that add value and help people in their everyday lives, or at the very least, to build things that help people’s lives suck a little less!

Come say hi, follow me on twitter!

Tagged , , ,
%d bloggers like this: