Skip to content

Instantly share code, notes, and snippets.

@vcabansag
Created July 11, 2013 18:45
Show Gist options
  • Save vcabansag/5978068 to your computer and use it in GitHub Desktop.
Save vcabansag/5978068 to your computer and use it in GitHub Desktop.
# Accessing hashes and arrays
# Create a set of hashes like below:
student1 = { "first" => "Neal", "last" => "Sales-Griffin", "hometown" => "Chicago" }
student2 = { "first" => "Mike", "last" => "McGee", "hometown" => "Freeport" }
student3 = { "first" => "Jeff", "last" => "Cohen", "hometown" => "Skokie" }
student4 = { "first" => "Raghu", "last" => "Betina", "hometown" => "Goshen" }
student5 = { "first" => "Vince", "last" => "Cabansag", "hometown" => "Sturgis" }
# Create a new array of students containing these hashes.
students = [student1, student2, student3, student4, student5]
# Create a hash that contains the States of the corresponding cities.
states = {"Chicago" => "Illinois", "Freeport" => "Illinois", "Skokie" => "Illinois",
"Goshen" => "Indiana", "Sturgis" => "Michigan"}
# Automate the task of displaying the students' names along with their
# hometowns and States. Use the .each method. Your output should look like this:
# Neal Sales-Griffin is from Chicago, Illinois.
# Mike McGee is from Freeport, Illinois.
# Jeff Cohen is from Skokie, Illinois.
# Raghu Betina is from Goshen, Indiana.
# Vince Cabansag is from Sturgis, Michigan.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment