Skip to content

Instantly share code, notes, and snippets.

@bryanaka
Created November 27, 2013 22:07
Show Gist options
  • Save bryanaka/7684060 to your computer and use it in GitHub Desktop.
Save bryanaka/7684060 to your computer and use it in GitHub Desktop.
hash_lesson for GA

Hash

The next data structure we are going to talk about is a hash. Hashes are fundamental in Ruby, and very important when it comes to Ruby Web Programing. You will see them used a lot in frameworks like Rails and Sinatra.

Basic Syntax

Here, I will introduce the basic syntax, then we will use a simple problem to compare how they are used in contrast to Arrays.

Key-Value Pairs

Arrays are created with a comma-separated list of values. In a similar fashion, Hashes are comprised of comma-separated Key-Value pairs.

Here is a basic hash.

{ :type => "Cat", :name => "Tabi", :age => 3 }

Hashes are created in a similar fashion to Arrays, except it uses curly braces instead of square brackets. Let's Explore a single Key-Value Pair inside the hash. Each pair is comprised of three parts:

:type => "Cat"

# Key   hashrocket   Value
:type      =>        "Cat"

Here, the key is the symbol :type. Symbols are commonly used as keys in a hash.

=> is known as a hashrocket. It is purly syntactical, and the way I like to visualize things is that the hashrocket is pointing the key to the value. A hashrokect isn't always required, and I will introduce alternative syntaxes later.

"Cat" is the value. Values in a hash can be anything. Integer, Symbol, Array, even another hash.

Accessing Values in a Hash

In arrays, you use the index to access a given value. S In Hashes, you use the key. So instead of using the index inside square brackets to access the value, just put the key in there, and it will return the corresponding value.

animal = { :type => "Cat", :name => "Tabi", :age => 3 }
animal[:type]
#=> "Cat"

For example, if you use the hash above and then use animal[:type], it will return the value "Cat".

Limitations of an Array

Before break, We learned that Arrays can hold many values inside a single variable and you can access the individual values using the array index.

The way I like to think of arrays are a list or collection of values.

Lets say we have an array of phone numbers. We will assume we know index 0 is Tom's Number.

phone_book = [ "1-555-555-5555", "1-111-111-1111", "1-800-636-6363" ]
phone_book[0]
#=> "1-555-555-5555"

But how can we keep track of this? We could set a variable called tom to 0, but that sort of defeats the purpose of the array. This also assumes that Tom's number will stay at index 0, which leaves this method of getting Tom's number fragile.

# Sort of defeats the purpose of using an array
# as you are now declaring two variables.
tom = 0
phone_book[tom]
#=> "1-555-555-5555"

# And is a fragile method of accessing Tom's number
phone_book.unshift("1-222-222-2222")
phone_book[tom] == "1-555-555-5555"
#=> false

Arrays are good at creating a list or collection, but not so good for keeping track of associations, like which number is associated to Tom.

This is where Hashes come in.

While Arrays are like lists or collections, Hashes are more like Dictionaries. You have a key, and you use that key to look up a value. With a hash, retriving Tom's Phone number becomes trivial.

phone_book = { :tom => "1-555-555-5555", :bryan => "1-111-111-1111" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment