• 20 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 2/6/20

Create and access hashes

Log in or subscribe for free to enjoy all this course has to offer!

 

It's time for the final Ruby data type of this course! 😅  Let's dive into hashes. 

A hash is very similar to an array in that it's a list of items. However, unlike an array, you do not access its contents with a number. You access it with a human-readable key that you define. 

Do you remember the table from the last chapter of an array that showed an index mapped to its contained element? We used the example of a list of fruits:

0

Apple

1

Pear

2

Orange

3

Banana

4

Kiwi

5

Pineapple

In hashes, you won't use numbers. You need to move one layer of abstraction up. A simple list of fruits won't work anymore. 

Instead, we'll define some favorites of our person from part 1 of this course: Arnaud! We'll define his favorite books, fruits, and colors as a hash. 

Visualized as a table, his favorites would look like this:

KEY

VALUE

books

Paris guidebook

 

"The Art of Innovation"

 -----

 -----

fruits

apples

 

oranges

 

pears

 -----

 -----

colors

yellow

 

green

On the left-hand side, you have something that categorizes the items on the right-hand side. In a hash, we call this a key:value pair.

Throw some curly braces in there, and you've got yourself a hash:

favorites = {
    books: ["Paris guidebook", "The Art of Innovation"],
    fruits: ["apples", "oranges", "pears"],
    colors: ["yellow", "green"],
}

In the above example, my hash is accessible under the variable namefavorites

Hash syntax

A hash always begins with a set of curly braces. Inside, you'll place your key value pairs. A key should end in a colon (ex.last_names:or zip_codes:), and the values can be any data type.

Overall, the general structure of a hash is as follows:

{ key: values,
key: values }

In the above example, there two key value pairs, but you can have as many as you like. 

Here are some sample hashes to solidify your knowledge:

state_capitals = {
    new_york: "Albany",
    pennsylvania: "Harrisburg",
    colorado: "Denver",
    maine: "Augusta",
    vermont: "Montpelier"
}

In this case, each state is a key, and its capital is a value in the form of a string. 

user = {
    first_name: "Emily",
    last_name: "Reese",
    age: 28,
    address: "123 Imaginary Road, New York, NY, 10014",
    picture: "photo.png",
    profile_quotes: ["If you can dream it, you can do it!", "Good vibes only."]
}

This hash contains the different attributes of a user. In fact, the people objects you created in part 1 of this course were hashes as well. 

  • The hash's first key is first_name, and its value is a string "Emily"

  • The hash's second key is last_name, and its value is a string "Reese".

  • The hash's third key is age, and its value is the number 28.

  • The hash's fourth key is address, and its value is a string"123 Imaginary Road, New York, NY, 10014".

  • The hash's fifth key is picture, and its value is a string "photo.png".

  • The hash's sixth key is profile_quotes, and its value is an array["If you can dream it, you can do it!", "Good vibes only."]

Accessing content in hashes

Accessing content in hashes is similar to the way you access content in arrays. Instead of passing the index as a number though, you pass the name of the key to the hash name itself.

 

 For example, to access the state capital of New York based on the array above, I could pass the key  new_york to the hash  state_capitals.

state_capitals[:new_york]
=> "Albany"

I would get the string  "Albany" as a result.

To access Arnaud's favorite books, I could pass the key  books to the hash  favorites:

favorites[:books]
=> ["Paris guidebook", "The Art of Innovation"]

I get the array ["Paris guidebook", "The Art of Innovation"] as a result. 

Use the interactive code exercise below:

https://www.codevolve.com/api/v1/publishable_key/2A9CAA3419124E3E8C3F5AFCE5306292?content_id=b55ba822-a212-4249-91dc-8d08abc37eba

You're all done with Ruby data types now! In the next chapter, let's take a little breather and talk about the Ruby community at large. 

Example of certificate of achievement
Example of certificate of achievement