Bird
0
0

You want to build a Ruby DSL that allows nested configuration like this:

hard📝 Application Q15 of 15
Ruby - Advanced Metaprogramming
You want to build a Ruby DSL that allows nested configuration like this:
config = Config.new do
  database do
    adapter 'sqlite3'
    pool 5
  end
  cache do
    enabled true
    size 64
  end
end
Which approach best supports this nested DSL pattern?
AUse global variables to store settings inside blocks
BUse class variables to share state across blocks
CUse instance_eval with separate classes for database and cache blocks
DUse eval with string interpolation for nested blocks
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested DSL needs

    Nested blocks require separate context objects for database and cache to hold their settings.
  2. Step 2: Identify best practice

    Using instance_eval on separate classes for each nested block cleanly isolates configuration and supports nested DSL.
  3. Final Answer:

    Use instance_eval with separate classes for database and cache blocks -> Option C
  4. Quick Check:

    Nested DSL = instance_eval + separate classes [OK]
Quick Trick: Use instance_eval with helper classes for nested blocks [OK]
Common Mistakes:
  • Using global variables causing conflicts
  • Using class variables mixing state
  • Using eval with strings risking security and complexity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes