Bird
0
0

Which of the following is the correct syntax to define a method that accepts a block in Ruby DSL?

easy📝 Syntax Q3 of 15
Ruby - Advanced Metaprogramming
Which of the following is the correct syntax to define a method that accepts a block in Ruby DSL?
Adef configure(block); yield; end
Bdef configure(&block); block.call; end
Cdef configure; yield block; end
Ddef configure(block); block.call; end
Step-by-Step Solution
Solution:
  1. Step 1: Understand block parameter syntax

    To accept a block as a parameter, use &block in method definition.
  2. Step 2: Check method body

    Calling block.call executes the passed block correctly.
  3. Final Answer:

    def configure(&block); block.call; end -> Option B
  4. Quick Check:

    Block parameter syntax = &block [OK]
Quick Trick: Use &block to capture blocks in method parameters [OK]
Common Mistakes:
  • Omitting & when defining block parameters
  • Using yield incorrectly with block variables
  • Passing block as normal argument without &

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes