Bird
0
0

Which is the correct syntax to define a method that uses a block in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Blocks, Procs, and Lambdas
Which is the correct syntax to define a method that uses a block in Ruby?
Adef example; yield; end
Bdef example() block.call end
Cdef example { yield } end
Ddef example; block(); end
Step-by-Step Solution
Solution:
  1. Step 1: Recall Ruby method and block syntax

    To call a block inside a method, use the keyword 'yield' without parentheses.
  2. Step 2: Check each option's syntax

    def example; yield; end uses 'yield' correctly. def example() block.call end tries to call 'block.call' without block parameter. def example { yield } end uses braces incorrectly in method definition. def example; block(); end calls 'block()' which is undefined.
  3. Final Answer:

    def example; yield; end -> Option A
  4. Quick Check:

    Correct block call = yield [OK]
Quick Trick: Use 'yield' to run a block inside a method [OK]
Common Mistakes:
  • Using braces in method definition
  • Calling undefined 'block' variable
  • Forgetting to use 'yield' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes