Bird
0
0

Why does block_given? return false inside a method that calls another method which yields to a block?

hard📝 Conceptual Q10 of 15
Ruby - Blocks, Procs, and Lambdas
Why does block_given? return false inside a method that calls another method which yields to a block?
ABecause blocks cannot be passed between methods in Ruby
BBecause <code>yield</code> disables block_given? in nested calls
CBecause <code>block_given?</code> only checks for blocks passed directly to the current method
DBecause the block is automatically converted to a Proc
Step-by-Step Solution
Solution:
  1. Step 1: Understand block_given? scope

    block_given? returns true only if the current method was called with a block.
  2. Step 2: Recognize block passing behavior

    If a method calls another method and yields there, the inner method does not see the block unless explicitly passed.
  3. Final Answer:

    Because block_given? only checks for blocks passed directly to the current method -> Option C
  4. Quick Check:

    block_given? checks only current method's block [OK]
Quick Trick: block_given? checks only current method's block presence [OK]
Common Mistakes:
  • Thinking blocks automatically pass between methods
  • Assuming yield disables block_given?
  • Confusing block and Proc conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes