Bird
0
0

Given a list of mixed values, how can you create a hash mapping each value to its class using Ruby's object model?

hard📝 Application Q8 of 15
Ruby - Basics and Runtime
Given a list of mixed values, how can you create a hash mapping each value to its class using Ruby's object model?
AUse values.map { |v| [v, v.class] }.to_h
BUse values.collect_class
CUse values.to_class_hash
DUse values.class_map
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want a hash where keys are values and values are their classes.
  2. Step 2: Use Ruby methods to build hash

    Mapping each value to an array of [value, value.class] then converting to hash with to_h achieves this.
  3. Final Answer:

    Use values.map { |v| [v, v.class] }.to_h -> Option A
  4. Quick Check:

    Map values to [value, class] pairs then to_h [OK]
Quick Trick: Use map with to_h to build hashes from arrays [OK]
Common Mistakes:
MISTAKES
  • Using non-existent methods like collect_class
  • Trying to call class_map on arrays
  • Confusing map with other methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes