Bird
0
0

In Ruby, what occurs when a method includes the yield statement?

easy📝 Conceptual Q1 of 15
Ruby - Blocks, Procs, and Lambdas
In Ruby, what occurs when a method includes the yield statement?
AThe method returns immediately without executing any block
BThe method pauses and executes the block passed to it
CThe method throws an error if no block is given
DThe method converts the block into a Proc object automatically
Step-by-Step Solution
Solution:
  1. Step 1: Understand yield

    The yield keyword transfers control from the method to the block passed to it.
  2. Step 2: Execution of block

    The block executes at the point where yield is called inside the method.
  3. Final Answer:

    The method pauses and executes the block passed to it -> Option B
  4. Quick Check:

    Method calls block with yield [OK]
Quick Trick: Yield runs the block passed to the method [OK]
Common Mistakes:
  • Thinking yield returns a Proc automatically
  • Assuming yield always causes an error if no block given
  • Believing yield skips block execution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes