Bird
0
0

Given a hash settings, how can you freeze all nested arrays inside it to prevent their modification, while keeping the hash itself mutable?

hard📝 Application Q9 of 15
Ruby - Functional Patterns in Ruby
Given a hash settings, how can you freeze all nested arrays inside it to prevent their modification, while keeping the hash itself mutable?
Asettings.freeze
Bsettings.map(&:freeze)
Csettings.each { |k, v| k.freeze if v.is_a?(Array) }
Dsettings.each_value { |v| v.freeze if v.is_a?(Array) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify nested arrays

    Use each_value to iterate over hash values and check if they are arrays.
  2. Step 2: Freeze only arrays

    Call freeze on values that are arrays to make them immutable.
  3. Final Answer:

    settings.each_value { |v| v.freeze if v.is_a?(Array) } -> Option D
  4. Quick Check:

    Freeze nested arrays, keep hash mutable = B [OK]
Quick Trick: Freeze nested arrays selectively with each_value and is_a? [OK]
Common Mistakes:
  • Freezing entire hash
  • Using map without assignment
  • Freezing keys instead of values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes