Skip to content

Instantly share code, notes, and snippets.

@M0119

M0119/_hashes.md Secret

Last active March 19, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0119/ecee95ed22c9cee192c3908f12b917a1 to your computer and use it in GitHub Desktop.
Save M0119/ecee95ed22c9cee192c3908f12b917a1 to your computer and use it in GitHub Desktop.

Lecture ✍️

Hashes and symbols

A hash is a data structure of key and value pairs.

This is how we create a hash.

zoo = {
    name: 'Melbourne Zoo', # key - name; value - string ('Melbourne Zoo)
    number_of_animals: 3290, # key - number_of_animals; value - integer (3290)
    kids_welcome: true, # key - kids_welcome; value - boolean (true),
    animals: ['tapir', 'snake', 'butterfly', 'tiger'] # key - animals; value - array of strings
}

This is how we access values within a hash.

  # How many animals?
  zoo[:number_of_animals]
  # Which animals 
  zoo[:animals]
  # One of those animals 
  zoo[:animals][1] # => 'snake'

Running through an example

We have seen how to make a hash, and how to access a hash, but what might be missing is a bit of the why. Lets say that we are building up a database of tennis players, and we want to store some information about them in a useful structure. The first step could be to create a hash directly.

federer = {
    name: 'Roger Federer',
    majors: 20,
    retired: true,
    home_country: 'Switzerland'
}

We might want to check that we have put these in correctly. Here we check that our 'name' key looks right.

puts(federer[:name])

Maybe we can also check that the 'retired' key is correct. I'll use another diagnostic puts:

puts(federer[:retired])

We see from this output that this doesn't look right (not just yet anyway). So we want to reassign the value of the key 'retired' within the variable 'federer' to false.

federer[:retired] = false

Let's make another hash.

nadal = {
    name: 'Raphael Nadal',
    majors: 17,
    retired: false,
    home_country: 'Spain'
}

We can check out what's inside doing this.

print nadal 
puts # (To add in the newline after the print)

..and one more hash

agassi = {
    name: 'Andre Aggasi',
    majors: 8,
    retired: true,
    home_country: 'USA'
}

Now we have three hashes that have the same format. We can see this by doing:

print federer
puts
print nadal 
puts 
print agassi
puts

Because they are the same, we can put these structures into another data structure. For example, we might store them in an array:

player_arr = [federer, nadal, agassi]

Now that we have an array, we have other tools at our disposal. Let's simply print out each player's name:

player_arr.each do |player|
    puts(player[:name])
end

Lets make another player, and then add them to the array:

djokovic = {
    name: 'Novak Djokovic',
    majors: 15,
    retired: false,
    home_country: 'Serbia'
}
player_arr << djokovic

Let's check that by running the previous loop.

player_arr.each do |player|
    puts(player[:name])
end

All seems to be in order.

Now we will up the complexity just a touch by adding in an if statement, and investigating our array further.

player_arr.each do |player|
    if(player[:retired])
        puts("#{player[:name]} has retired")
    else
        puts("#{player[:name]} still on the circuit")
    end
end

We could also start getting overconfident about Nadal at the French Open and decide to push up his 'majors' value by one even before the start of the tournament. There are many ways to do this, but we are going to manually reach into the array and perform this action. We will just check we are reaching the right point first.

puts player_arr[1][:name]
puts player_arr[1][:majors]
player_arr[1][:majors] += 1
puts player_arr[1][:majors]

Resources

# Using hashes
# 1. Define three hashes that each have two keys. Make the value of the keys numbers. Access a value within each of these hashes and print to screen.
# 2. Extract breakfast from the following hash and print to screen
# meal_plan = {
# breakfast: 'weet-bix',
# lunch: 'sandwich',
# dinner: 'chicken salad'
# }
# 3. Define three hashes that have three keys in each and set the values to be of three different data types.
# 4. Print to screen the value and the datatype of that value.
# 5. Create a dictionary using hash. Make the keys to be words and values to be meanings of those words.
# 6. Access some words from the dictionary and print the meaning to screen.
# 7. Change the values to be arrays with more than one synonym of the word.
# 8. Select one word and print to screen all the synonyms using a loop.
# 9. Make an array of users. Each user will be a hash with the keys name, age and email.
# 10. Loop through this array, printing to screen the value of each member of the array.
# 11. Make a hash of all the ruby data types where the keys will be the name of the datatype and the value will be an example of that datatype, e.g. string: "this is a string".
# 12. Make an array of cars, each car will be a hash with keys make, model and year.
# 13. Loop through this array, printing to screen the value of each member of the array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment