Learn Ruby the Hard Way – Exercise 39 Hashes, oh lovely Hashes

Here is my work for Exercise 39.

My code is on github.

I went through the first part of the exercise with no problems, but got stuck on the “Make your Own Hash Module” section for a while. I felt overwhelmed as I was typing the code, not sure what was going on. I eventually just kept typing without thinking each line through. After I finished typing the code, I tried to just read through the dict.rb file, but was confused.

I procrastinated… by talking through and helping a friend solve her CoderByte problem. At least my procrastination was still programming…

Anyways, I finally decided to get some brain food (chicken and cheese crepe!) and get back to it. When in doubt, comment everything! By going through each line carefully and adding comments, I finally grasped what all the methods in the module do.

Lesson, when confronted with something that intimidates you: break it down into smaller chunks and comment ALL THE THINGS. 😉

Comment all the things!
Comments, comments everywhere!

Study Drills
1. Do this same kind of mapping with cities and states/regions in your country or some other country.

Screen Shot 2015-09-24 at 5.19.04 PM
Mapping Canada!
Information about the provinces and territories.
Information about the provinces and territories.

2. Find the Ruby documentation for hashes and try to do even more things to them.
Ruby documentation on hashes. I use the select method on the provinces hash to find which provinces have long names (length greater than 10 characters).

Finding long province names
Finding long province names
I list the long province and territory names.
I list the long province and territory names.

3. Find out what you can’t do with hashes. A big one is that they do not have order, so try playing with that.
Although hashes do not have order, you can sort a hash. Calling the sort method on a hash will put them in the alphabetical order of the keys.

4. Read about Ruby’s assert feature and then take the Dict code and add assertions for each of the tests I’ve done instead of print. For example, you can assert that the first get operation returns “Flamenco Sketches” instead of just printing that out.
This study drill was very confusing. To use assert, we need to include this at the beginning of the script

require ‘test/unit’
extend Test::Unit::Assertions

then we can assert(condition, [failure_message]).

Nowhere in the code do we ever print “Flamenco Sketches” so I’m not sure where we are supposed to return “Flamenco Sketches”….

5. Did you notice that the list function listed the items I added in a different order than they were added? This is an example of how hashes don’t maintain order, and if you analyze the code you’ll understand why.
The hashes are not added to buckets in order, that’s why they will not be listed in the order they are added.

6. Create a dump function that is like list but which dumps the full contents of every bucket so you can debug it.
Screen Shot 2015-09-24 at 5.48.00 PM
The dump method puts all the buckets to the screen for debugging. To improve this code, we can add a if bucket, so that the empty buckets are ignored.

7. Make sure you know what the hash function does in that code. It’s a special function that converts strings to a consistent integer. Find the documentation for it online. Read about what a “hash function” is in programming.
The hash function takes a string argument and returns a consistent integer. There can be collisions (where different strings get mapped to the same hash value). The hash value is representative of the original string of characters.

Summary
This was a lot to take in for one exercise! In this exercise I learned about hashes and hash functions. I learned how to create a Dictionary module and put “buckets” into a Dictionary array. By using a hash function, keys could be mapped to these buckets. Methods could be written to retrieve bucket arrays, get specific key-values pairs (slots) from buckets, and set or delete specific slots.

This setup can be thought of like a real dictionary: in a dictionary you have sections by alphabet (a-z) and then in each section, you have individual slots or words. Each word has a key-value pair, with a word and a definition.

 

Advertisements

3 thoughts on “Learn Ruby the Hard Way – Exercise 39 Hashes, oh lovely Hashes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s