Bird
0
0

Identify the error in this Ruby DSL method definition:

medium📝 Debug Q6 of 15
Ruby - Advanced Metaprogramming
Identify the error in this Ruby DSL method definition:
def setup(&block)
  instance_eval(block)
end
AMethod should use yield instead of instance_eval
BBlock parameter should be omitted for instance_eval
Cinstance_eval cannot be used with blocks
DPassing block without calling it causes error; should use instance_eval(&block)
Step-by-Step Solution
Solution:
  1. Step 1: Understand instance_eval usage

    instance_eval expects a block passed with &block syntax, not a block object as argument.
  2. Step 2: Identify the error

    Passing block as instance_eval(block) passes a Proc object as argument, which is incorrect.
  3. Final Answer:

    The correct usage is instance_eval(&block) -> Option D
  4. Quick Check:

    Block must be passed with & to instance_eval [OK]
Quick Trick: Use &block to pass block to instance_eval [OK]
Common Mistakes:
  • Passing block without & causes ArgumentError
  • Confusing block parameter with block call
  • Using yield instead of instance_eval incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes