Bird
Raised Fist0
Rubyprogramming~10 mins

Hash methods (keys, values, each) in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get all keys from the hash.

Ruby
my_hash = {a: 1, b: 2, c: 3}
keys = my_hash.[1]
puts keys
Drag options to blanks, or click blank then click option'
Akeys
Bvalues
Ceach
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'values' instead of 'keys' returns the values, not keys.
Using 'each' returns an enumerator, not an array of keys.
2fill in blank
medium

Complete the code to get all values from the hash.

Ruby
my_hash = {x: 10, y: 20, z: 30}
vals = my_hash.[1]
puts vals
Drag options to blanks, or click blank then click option'
Akeys
Bvalues
Ceach_key
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'keys' returns keys, not values.
Using 'each_key' returns an enumerator for keys, not values.
3fill in blank
hard

Fix the error in the code to print each key and value pair.

Ruby
my_hash = {name: 'Alice', age: 30}
my_hash.[1] do |key, value|
  puts "#{key}: #{value}"
end
Drag options to blanks, or click blank then click option'
Akeys
Bvalues
Ceach
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'keys' or 'values' returns arrays, not pairs.
Using 'map' returns a new array, not just iteration.
4fill in blank
hard

Fill both blanks to create a hash of word lengths for words longer than 3 letters.

Ruby
words = ['cat', 'house', 'tree', 'a', 'dog']
lengths = {}
words.each do |word|
  if word.length [2] 3
    lengths[word] = word[1]
  end
end
puts lengths
Drag options to blanks, or click blank then click option'
A.length
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' filters shorter words.
Forgetting to use '.length' to get word length.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase keys and values greater than 10.

Ruby
data = {"a": 5, "b": 15, "c": 25}
result = {}
data.[3] do |k, v|
  if v > 10
    result[[1]] = [2]
  end
end
puts result
Drag options to blanks, or click blank then click option'
Ak.upcase
Bv
Ceach
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upcase' keeps keys lowercase.
Using 'map' instead of 'each' changes the return type.

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