Tag Archives: Chris Pine

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 , , , , ,
%d bloggers like this: