- Practice instantiating hashes
- Practice adding key/value pairs to hashes
- Practice retrieving information from hashes
Refer back to the previous lesson to help you solve this lab!
-
In the
my_hash
method, use the literal constructor to set a variable calledmy_hash
equal to a hash with key/value pairs of your choice. -
Remember, key/values pairs are associative. It doesn't make sense to have a key/value pair of
"my_name" => 78
. It does make sense to have a key/value pair of"my_name" => "Herman Melville"
(if you happen to be that author, of course).
- In the
shipping_manifest
method, set a variable calledshipping_manifest
equal to a hash. - fill that hash with key/value pairs that describe the following information:
- We have 5 whale bone corsets, 2 porcelain vases and 3 oil paintings. Your hash should have the following comma separated key/value pairs:
"whale bone corsets" => 5
"porcelain vases" => 2
"oil paintings" => 3
- We have 5 whale bone corsets, 2 porcelain vases and 3 oil paintings. Your hash should have the following comma separated key/value pairs:
- Note: Build this hash yourself! Don't just copy and paste from the following methods. :)
- In the
retrieval
method we've given you the shipping_manifest hash that you built out in the previous challenge. Use the[]
hash method described in the previous readme to return the value of the"oil paintings"
key of theshipping_manifest
hash. - For example, if we have the following hash:
hash = {"key1" => "value1"}
We can return the value of "key1"
like this:
hash["key1"]
- In the
adding
method, we've once again given you the shipping_manifest hash. Use the[]=
method to add the following key/valuve pair to the hash:"pearl necklace" => 1
- This method must return the newly-added-to
shipping_hash
. - Remember that the return value of using the
[]=
method is the value of the key/value pair you added. We need ouradding
method to return theshipping_manifest
. - Here's a refresher on the
[]=
method:
hash = {"key1" => "value1"}
hash["key2"] = "value2"
# returns "value2"
puts hash
# => {"key1" => "value1", "key2" => "value2"}