Bird
0
0

How can you create a hash from two arrays in Ruby, where one array contains keys and the other contains values?

hard📝 Application Q8 of 15
Ruby - Basics and Runtime
How can you create a hash from two arrays in Ruby, where one array contains keys and the other contains values?
AUse keys.map(values)
BUse Hash[keys.zip(values)]
CUse keys + values
DUse keys.merge(values)
Step-by-Step Solution
Solution:
  1. Step 1: Understand zip method

    zip combines two arrays element-wise into pairs.
  2. Step 2: Convert pairs to hash

    Hash[] converts array of pairs into a hash with keys and values.
  3. Final Answer:

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

    Combine arrays with zip, then convert to hash [OK]
Quick Trick: Use zip to pair arrays, then Hash[] to create hash [OK]
Common Mistakes:
  • Trying to add arrays directly
  • Using map incorrectly
  • Using merge on arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes