Bird
0
0

In Ruby, what is the purpose of the block_given? method when called inside a method?

easy📝 Conceptual Q1 of 15
Ruby - Blocks, Procs, and Lambdas
In Ruby, what is the purpose of the block_given? method when called inside a method?
AIt returns <code>true</code> if the method was called with a block, otherwise <code>false</code>.
BIt executes the block passed to the method.
CIt converts the block into a Proc object.
DIt checks if the method has any parameters.
Step-by-Step Solution
Solution:
  1. Step 1: Understand block_given?

    This method returns a boolean indicating whether a block was passed to the current method.
  2. Step 2: Differentiate from other block-related methods

    It does not execute or convert the block; it only checks for its presence.
  3. Final Answer:

    It returns true if the method was called with a block, otherwise false. -> Option A
  4. Quick Check:

    Check if a block is passed without executing it [OK]
Quick Trick: block_given? returns true if block passed [OK]
Common Mistakes:
  • Confusing block_given? with yield
  • Assuming block_given? executes the block
  • Thinking block_given? converts block to Proc

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes