Bird
Raised Fist0
Rubyprogramming~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. What does the keys method do when called on a Ruby hash?
      easy
      A. Deletes all keys from the hash
      B. Returns an array of all the values in the hash
      C. Returns an array of all the keys in the hash
      D. Iterates over each key-value pair in the hash

      Solution

      1. Step 1: Understand the keys method

        The keys method extracts all keys from the hash and returns them as an array.
      2. Step 2: Compare with other methods

        values returns values, each iterates pairs, so only keys returns keys array.
      3. Final Answer:

        Returns an array of all the keys in the hash -> Option C
      4. Quick Check:

        keys = keys array [OK]
      Hint: Remember: keys method lists all keys as an array [OK]
      Common Mistakes:
      • Confusing keys with values method
      • Thinking keys modifies the hash
      • Mixing up keys with each method
      2. Which of the following is the correct syntax to iterate over each key-value pair in a Ruby hash h?
      easy
      A. h.each(key, value) { puts key, value }
      B. h.each { |key, value| puts key + value }
      C. h.each do |key value| puts key, value end
      D. h.each { key, value -> puts key + value }

      Solution

      1. Step 1: Identify correct block syntax for each

        The correct syntax uses curly braces or do-end with block parameters separated by a comma inside pipes: { |key, value| ... }.
      2. Step 2: Check each option

        h.each { |key, value| puts key + value } uses correct syntax with pipes and comma. Others have syntax errors or wrong block format.
      3. Final Answer:

        h.each { |key, value| puts key + value } -> Option B
      4. Quick Check:

        Correct block syntax = h.each { |key, value| puts key + value } [OK]
      Hint: Use pipes and commas inside block for key-value pairs [OK]
      Common Mistakes:
      • Omitting pipes around block parameters
      • Missing comma between key and value
      • Using arrow syntax incorrectly in blocks
      3. What will be the output of the following Ruby code?
      h = {a: 1, b: 2, c: 3}
      h.keys
      medium
      A. [:1, :2, :3]
      B. [1, 2, 3]
      C. {a: 1, b: 2, c: 3}
      D. [:a, :b, :c]

      Solution

      1. Step 1: Understand keys method on hash

        The keys method returns an array of all keys in the hash. Here keys are symbols :a, :b, :c.
      2. Step 2: Match output with options

        [:a, :b, :c] matches the array of keys. [1, 2, 3] is values, C is the whole hash, D is invalid symbol array.
      3. Final Answer:

        [:a, :b, :c] -> Option D
      4. Quick Check:

        h.keys = [:a, :b, :c] [OK]
      Hint: keys returns array of keys exactly as in hash [OK]
      Common Mistakes:
      • Confusing keys with values output
      • Expecting hash instead of array
      • Misreading symbols as strings
      4. Identify the error in the following Ruby code snippet:
      h = {x: 10, y: 20}
      h.each do |k v|
        puts "#{k}: #{v}"
      end
      medium
      A. Missing comma between block parameters k and v
      B. Hash keys must be strings, not symbols
      C. The each method cannot be used with do-end
      D. The puts statement syntax is incorrect

      Solution

      1. Step 1: Check block parameter syntax

        Block parameters must be separated by a comma inside pipes: |k, v|. Here comma is missing.
      2. Step 2: Verify other parts

        Keys as symbols are valid, each do-end is valid, and puts syntax is correct.
      3. Final Answer:

        Missing comma between block parameters k and v -> Option A
      4. Quick Check:

        Block params need comma [OK]
      Hint: Always separate block parameters with commas inside pipes [OK]
      Common Mistakes:
      • Omitting comma between block variables
      • Thinking symbols are invalid keys
      • Confusing block syntax with method call
      5. Given the hash h = {a: 1, b: 0, c: nil, d: 4}, which code snippet correctly returns an array of keys whose values are truthy (not false or nil)?
      hard
      A. h.select { |k, v| v }.keys
      B. h.keys.select { |k| k }
      C. h.values.select { |v| v }.keys
      D. h.each { |k, v| k if v }.keys

      Solution

      1. Step 1: Understand filtering keys by truthy values

        We want keys where values are truthy (not false or nil). select filters pairs by value truthiness.
      2. Step 2: Analyze each option

        h.select { |k, v| v }.keys filters hash pairs by truthy value, then gets keys -> [:a, :d] [check]. h.keys.select { |k| k } checks if key truthy (all symbols are), returns all keys. h.values.select { |v| v }.keys calls keys on array (NoMethodError). h.each { |k, v| k if v }.keys returns original hash, no filtering.
      3. Final Answer:

        h.select { |k, v| v }.keys -> Option A
      4. Quick Check:

        Filter pairs then get keys = h.select { |k, v| v }.keys [OK]
      Hint: Use select on hash then call keys to filter by values [OK]
      Common Mistakes:
      • Trying to call keys on array of values
      • Using each instead of select for filtering
      • Selecting keys without checking values
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard