Bird
0
0

Identify the error in this Ruby method:

medium📝 Debug Q14 of 15
Ruby - Blocks, Procs, and Lambdas
Identify the error in this Ruby method:
def show
  yield if block_given
end

show { puts "Hello!" }
AMissing question mark in block_given method call
Byield cannot be used without arguments
CMethod show requires a parameter
DBlock cannot be passed to show method
Step-by-Step Solution
Solution:
  1. Step 1: Check block_given usage

    The method block_given? requires the question mark and should be called as block_given? or block_given?(). Here it is missing the question mark.
  2. Step 2: Confirm yield usage

    The yield is correctly used and does not require arguments here. The method does not require parameters to accept a block.
  3. Final Answer:

    Missing question mark in block_given method call -> Option A
  4. Quick Check:

    block_given? needs question mark [OK]
Quick Trick: Always use block_given? with question mark [OK]
Common Mistakes:
  • Omitting question mark in block_given
  • Thinking yield needs arguments always
  • Assuming method needs parameters for blocks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes