Jump start: Lesson 13
- Vocabulary: data structure, hash, key-value pair, default value
- To understand what a data structure is and how it can be used
- To be able to create a new hash data structure
- To be able to access data in a hash
- To be able to add data to a hash
- To understand the difference between an array and a hash
- Review the notes in this section
- Complete Hash Worksheet
- Complete Account Generator Continued
- Complete Walk-a-thon
A Hash
is another very popular collection type in Ruby. It is similar to an Array, except that indexing is done via unordered keys of any object type, and not an ordered integer index.
{ "key1" => "key1sAssociatedValue", "key2" => "key2sAssociatedValue" ...}
We initialize an empty Hash
by using {}
(pronounced brace). A hash can be assigned to a variable in the same or a similar way that other types of data are assigned to a variable:
my_hash = {}
my_hash = Hash.new
We know that this hash is empty because the hash definition starts with the {
(left brace) and ends with the }
(right brace), and there is nothing between those two symbols.
By using the new
syntax and specifying a default value, all keys retrieved with no corresponding value will return the default value. Without the default value, accessing an undefined key will return nil
.
my_hash = Hash.new
my_hash["gibberish"] # => nil
default_data = Hash.new("def")
# By creating a hash with a default value,
# all data retrievals will return the default value if not specified otherwise
default_data["gibberish"] # => "def"
You can also create a new hash with key/value pairs populated.
my_dog = {
"name" => "barkly",
"breed" => "beagle",
"age" => 2
}
To access data from within a hash, we use a syntax that is similar to accessing data in an Array. The difference is that we use the strings that correspond to the key to retrieve the associated value.
my_dog["age"] # => 2
my_dog["breed"] # => "spaniel"
my_dog["name"] # => "barkly"
# by default, will return nil for keys that don't exist
my_dog["color"] # => nil
We can utilize our understanding of reassigning values in Arrays to assign and reassign values in a Hash.
Assuming that we want to add a new key value pair to an existing hash, we can choose the key that we want to set the value for. In the case below, we have decided that we want to set the value "blenheim"
for a new key "color"
.
# Add a new key value pair for color
my_dog["color"] = "blenheim"
In addition, we can reset the value associated with an existing key using the same syntax. In the case below, we would like to set the "age"
value to 3
from it's original 2
.
# Retrieve the value currently set
my_dog["age"] # => 2
# Reassign the value associated with the age key
my_dog["age"] = 3
# Retrieve the updated value
my_dog["age"] # => 3
This method returns the number of items in the hash. An item corresponds to a key/value pair.
my_dog = {
"name" => "barkly",
"breed" => "beagle",
"age" => 2
}
my_dog.length # => 3
This method returns an array of all the defined keys for the hash.
my_dog.keys #=> ["name", "breed", "age"]
This method returns an array of all the values of all defined keys for the hash.
my_dog.values #=> ["barkly", "beagle", 3]