- Build simple hashes.
- Access and modify data stored in a hash.
- Add new data to a hash.
It's time to practice what we've learned about Ruby hashes. In this lab, your
task is to implement a set of methods. Each method will require you to complete
some basic hash creation and modification. Use learn
and follow the test
suite messages as you work through your solution.
-
Create a method called
new_hash
that creates and returns a new, empty hash. -
Create a method called
my_hash
that returns a valid hash. This hash should have at least one key/value pair of your choice. -
Create a method called
pioneer
that returns a hash. This hash should have a key that is a symbol:name
and whose value is a string,'Grace Hopper'
. -
Create a method called
id_generator
that creates and returns a hash with one key/value pair. The key should be a symbol,:id
. For the value, provide a positive integer of your choice. -
Create a method called
my_hash_creator
that takes in two parameters. In this method, create and return a hash with one key/value pair, using the first parameter as the key, and the second parameter as the value. -
Create a method call
read_from_hash
that takes in two parameters. The first parameter is a hash, the second is a key. Used together, they will either produce a value on that hash corresponding to the key, ornil
by default. Use these two parameters in conjunction to do just that. -
Create a method called
update_counting_hash
that takes in two parameters. The first parameter is a hash, and the second is a key. The hash provided will have any number of keys, but all values will be integers. Theupdate_hash
method should do one of two things: increment an existing value based on the provided key parameter, or create a new key/value pair using the provided key, setting the value to1
. So, for example, if we had the following hash:
{
:apples => 3
}
..and a provided key, :apples
, update_hash
should return an updated
hash:
{
:apples => 4
}
However, if the hash was given a key that doesn't exist, like :oranges
, it
should return:
{
:apples => 3,
:oranges => 1
}
Hashes are an essential data type that we will use frequently. Now that you've seen the basics of creating, reading and updating hashes, we can explore some examples of more complicated hashes!