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:
Step 1: Recall Ruby method and block syntax
To call a block inside a method, use the keyword 'yield' without parentheses.
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.
Final Answer:
def example; yield; end -> Option A
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
Master "Blocks, Procs, and Lambdas" in Ruby
9 interactive learning modes - each teaches the same concept differently