Recall & Review
beginner
What does the
keys method do in a Ruby hash?It returns an array containing all the keys from the hash.
Click to reveal answer
beginner
What does the
values method return when called on a Ruby hash?It returns an array of all the values stored in the hash.
Click to reveal answer
beginner
How does the
each method work on a Ruby hash?It goes through each key-value pair in the hash and lets you run code for each pair.
Click to reveal answer
beginner
Given
h = {a: 1, b: 2}, what is the output of h.keys?[:a, :b]
Click to reveal answer
intermediate
Explain the difference between
each and each_key methods in Ruby hashes.each iterates over key-value pairs, while each_key iterates only over keys.Click to reveal answer
What does
hash.values return?✗ Incorrect
values returns all the values stored in the hash as an array.Which method lets you run code for each key-value pair in a Ruby hash?
✗ Incorrect
each iterates over each key-value pair allowing you to run code on them.What is the output of
{x: 10, y: 20}.keys?✗ Incorrect
keys returns an array of the keys, which are :x and :y.If you want only the keys from a hash, which method do you use?
✗ Incorrect
The
keys method returns only the keys from the hash.What does the
each method yield to the block when called on a hash?✗ Incorrect
each yields both key and value to the block, usually as two parameters.Describe how you would use the
each method to print all keys and values of a hash.Think about how a hash stores pairs and how each lets you access them.
You got /3 concepts.
Explain the difference between
keys and values methods in Ruby hashes.Focus on what part of the hash each method extracts.
You got /3 concepts.