Bird
Raised Fist0
Rubyprogramming~20 mins

Hash methods (keys, values, each) in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output of keys and values methods
What is the output of this Ruby code?
Ruby
h = {a: 1, b: 2, c: 3}
puts h.keys.inspect
puts h.values.inspect
A
[:a, :b, :c]
["1", "2", "3"]
B
["a", "b", "c"]
[1, 2, 3]
C
["a", "b", "c"]
["1", "2", "3"]
D
[:a, :b, :c]
[1, 2, 3]
Attempts:
2 left
💡 Hint
Remember that Ruby symbols are shown with a colon and strings with quotes.
❓ Predict Output
intermediate
1:30remaining
Using each to iterate over a hash
What does this Ruby code print?
Ruby
h = {x: 10, y: 20}
h.each do |key, value|
  puts "#{key}:#{value}" 
end
A
10:x
20:y
B
x:10
y:20
C
[:x, 10]
[:y, 20]
D
x => 10
y => 20
Attempts:
2 left
💡 Hint
Look at how the block variables are used inside the string.
🧠 Conceptual
advanced
1:00remaining
Behavior of keys method on empty hash
What is the result of calling keys on an empty hash in Ruby?
AAn empty array []
Bnil
CRaises an error
DAn array with one nil element [nil]
Attempts:
2 left
💡 Hint
Think about what keys exist in an empty hash.
❓ Predict Output
advanced
1:30remaining
Effect of modifying hash inside each
What is the output of this Ruby code?
Ruby
h = {a: 1, b: 2}
h.each do |k, v|
  h[k] = v * 2
end
puts h.inspect
A{:a=>1, :b=>4}
B{:a=>1, :b=>2}
C{:a=>2, :b=>4}
DRuntimeError: can't modify hash during iteration
Attempts:
2 left
💡 Hint
Think about whether modifying values during iteration is allowed.
❓ Predict Output
expert
2:00remaining
Output of nested each with keys and values
What is printed by this Ruby code?
Ruby
h = {one: [1, 2], two: [3, 4]}
h.each do |key, values|
  values.each do |v|
    puts "#{key}-#{v}"
  end
end
A
one-1
one-2
two-3
two-4
B
one-1
two-2
one-3
two-4
C
one-1
one-2
one-3
one-4
D
1-one
2-one
3-two
4-two
Attempts:
2 left
💡 Hint
Look carefully at the nested iteration and string interpolation.

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