Bird
0
0

Given this array of keys and values:

hard📝 Application Q8 of 15
Ruby - Hashes
Given this array of keys and values:
keys = [:a, "b", :c]
values = [1, 2, 3]

Which code correctly creates a hash with symbol and string keys from these arrays?
Akeys.to_h(values)
B{ keys => values }
CHash[keys.zip(values)]
DHash.new(keys, values)
Step-by-Step Solution
Solution:
  1. Step 1: Understand zip and Hash[] usage

    keys.zip(values) pairs each key with its value, creating an array of pairs. Hash[] converts this array into a hash.
  2. Step 2: Check other options

    Options B, C, and D use invalid syntax or methods that don't exist for this purpose.
  3. Final Answer:

    Hash[keys.zip(values)] -> Option C
  4. Quick Check:

    Use Hash[keys.zip(values)] to create hash from arrays [OK]
Quick Trick: Use Hash[keys.zip(values)] to combine arrays into hash [OK]
Common Mistakes:
MISTAKES
  • Trying to use to_h with arguments
  • Passing arrays directly as hash keys
  • Using Hash.new incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes