0
0
Rubyprogramming~5 mins

Hash methods (keys, values, each) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe first key-value pair
BAn array of all keys in the hash
CThe number of key-value pairs
DAn array of all values in the hash
Which method lets you run code for each key-value pair in a Ruby hash?
Akeys
Beach
Cvalues
Dlength
What is the output of {x: 10, y: 20}.keys?
A[:x, :y]
B[10, 20]
C[:10, :20]
DAn error
If you want only the keys from a hash, which method do you use?
Akeys
Bselect
Ceach
Dvalues
What does the each method yield to the block when called on a hash?
AOnly the key
BOnly the value
CBoth key and value as two block parameters
DAn array of keys
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.