Category Archives: What i learned today

What I learned today: Not All Methods Are Created Equal

Today I wrote a script in Ruby that, among other things, receives a text file as input and calculates the word count. One of the things I love about Ruby is that there’s always more than one method for doing things. But what I learned today is, not all methods are created equal. Let’s say we wanted to count the words in the following text:

What an excellent example of the power of dress, young Oliver
Twist was! Wrapped in the blanket which had hitherto formed his
only covering, he might have been the child of a nobleman or a
beggar; it would have been hard for the haughtiest stranger to
have assigned him his proper station in society. But now that he
was enveloped in the old calico robes which had grown yellow in
the same service, he was badged and ticketed, and fell into his
place at once–a parish child–the orphan of a workhouse–the
humble, half-starved drudge–to be cuffed and buffeted through
the world–despised by all, and pitied by none.

One way to accomplish this task is by using the scan method and passing the regular expression /\w+/ as an argument. scan iterates over a string and looks for a certain pattern passed to it as an argument, then outputs any matches into an array. So let’s say we store the Oliver Twist text above in a variable named text, and use the scan method to search for any word character using regular expressions, then ask to return the number of words found. Here’s how that would look:

Ruby scan method

Here, the scan method searched for all alphanumeric characters then returned the results into an array. The length method returns the number of words found. In this case, 113 words.

Another method we can use to count how many words are stored in the text variable is to use the split method. When no arguments are passed to the split method, it will automatically split the string by whitespace and return the results in an array. Passing the length method to that result will also return the number of words stored in the text variable. This is what that would look like:

Ruby split method

The split method returned only 107 words. Do you know why this may be? The reason is that by passing the regular expression, /\w+/, as an argument, the scan method counted the hyphenated words as two words, when they should have only been counted as one. So it seems to me that using split can provide a more accurate method to determine word count.

What do you think? Do you agree that using the split method can provide a more accurate word count, or can you use regular expressions to achieve the same result? Leave a comment below, I’d love to hear your thoughts.

Advertisements
Tagged , , , ,

Tutorial: Event Manager – by JumpstartLab

Today I built a program in Ruby called Event Manager that takes a CSV file containing ACTUAL attendee data from a political conference and manipulates that data based on certain instructions. If you’re new to Ruby or programming for that matter, I highly recommend it. Among other things, you’ll learn how to work with the CSV library and how to access a web-based API. The data file contains attendee names, addresses, phone numbers, emails, and the date and time they registered for the event. Problem #1 to solve was removing a lot of junk (incorrect street addresses and phone numbers). Here I learned a lot about string manipulation techniques. Then I learned how to output the cleaned data to a new CSV file. The fun part started when accessing the Sunlight API from the Sunlight Foundation, here I learned how to create a method called rep_lookup that looks up legislator data for each attendee based on their zipcode. I also learned how to create a method that pulls an attendee’s name and address from the original CSV file and creates a custom form letter in HTML. Awesome! Lastly, I learned how to do some analysis. I created methods that determined which days of the week and hours of the day most people registered for the event. This can be useful when determining the best times to run ads for a future event. All in all, this is a great tutorial for those wanting to learn Ruby. Check out the source code on my Github and the tutorial here.

Tagged , , , , ,

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 , , , , ,

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