SHARE:
Uncategorized

Problem Solving and Iteration – Week 3

Flatiron School / 7 October 2013

The following is a guest post by Ian Miller and originally appeared on his blog. Ian is currently in the Ruby-003 class at The Flatiron School. You can follow him on Twitter here.

Last week, we dove right into database iteration and management. We focused on the following: SQL, hash iteration, web scraping (via Nokogiri, Open-URI), and logic approaches.

SQL and hash iteration are pretty straightforward as concepts, but they are difficult (for the time being) for me to effectively implement. We had to implement these concepts in the web-scraping exercise we did last week, and I still have to go back over and do it again, just to make sure I get the concepts down pat.

So, to explain: SQL is a database tool that can be used to integrate data from hashes and arrays into organized tables. You can also use SQL to join tables together where they share common information (usually something like an ID or user ID — keys that can tie two charts together). the exercise that we did last week focused on Reddit. We used Open-URI to open a connection to Reddit’s front page, and Nokogiri pulled down the HTML data into a readable, concise format. We then used Nokogiri’s syntax to sort out the data that we needed for the exercise (Top 100 posts: name of post, URL of post origin, upvotes, downvotes).

In order to create SQL tables, you use the following command:

This just creates the tables with the variables var1, var2, var3, and they are of data type TYPE (which can be INTEGER, TEXT, DATE, etc).

To insert values into the table, you use the following command:

To follow up with that, there are JOINS statements: INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, AND FULL OUTER JOIN.

Moving on to Hashes. Hashes are one of the most fundamental concepts in the Ruby language, and enable a wide array of possibilities if they are properly utilized. In other languages, the hash structure is referred to as a dictionary. It has a key-value pair, so for each respective key, there is a value, just as in a dictionary, there is a definition for each word.

The way to visualize a hash is via the following:

How do you access the value for ‘dog’? By simply typing in hash[:dog]. This will return the string ‘canine’. How do you add a new element to the hash? By simply typing in the key you want into the key field, and setting it equal to its value. For example, hash[:elephant] = ‘mammal’ will insert :elephant => ‘mammal’ into the hash. Finally, the default return value of a hash is nil.

My objective for this week is to work on further solidifying my understanding of hashes, how to iterate through them effectively, and ultimately get a web-scraping objective finalized.

Previous Post Git Cookin' With Git's Octopress Next Post