Bird
0
0

Identify the problem in this code snippet:

medium📝 Debug Q7 of 15
Ruby - Blocks, Procs, and Lambdas
Identify the problem in this code snippet:
def example
  yield
  yield(5)
end

example { |x| puts x }
AError because first yield has no argument but block expects one
BSyntax error in yield calls
CNo problem, code runs and prints twice
DBlock ignores argument and prints nothing
Step-by-Step Solution
Solution:
  1. Step 1: Analyze yield calls and block parameters

    The first yield calls block with no argument, second with 5.
  2. Step 2: Understand block parameter expectations

    The block expects one parameter x. The first yield passes none, but Ruby assigns nil to x, so no error occurs.
  3. Final Answer:

    No problem, code runs and prints twice -> Option C
  4. Quick Check:

    Yield with missing args passes nil [OK]
Quick Trick: Yield arguments can be nil if missing [OK]
Common Mistakes:
  • Calling yield with wrong number of arguments
  • Ignoring block parameter count
  • Assuming block handles missing args gracefully

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes