Bird
0
0

What is wrong with this Ruby code?

medium📝 Debug Q6 of 15
Ruby - Blocks, Procs, and Lambdas
What is wrong with this Ruby code?
def display
  yield 'Ruby'
end

display
AIt prints 'Ruby' without errors
BIt raises a LocalJumpError because no block is given
CIt returns nil silently
DIt causes a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Method calls yield with argument

    The method expects a block to be passed to execute yield 'Ruby'.
  2. Step 2: No block given

    Calling display without a block causes Ruby to raise LocalJumpError.
  3. Final Answer:

    It raises a LocalJumpError because no block is given -> Option B
  4. Quick Check:

    Yield without block causes LocalJumpError [OK]
Quick Trick: Always pass a block when using yield [OK]
Common Mistakes:
  • Assuming yield works without a block
  • Expecting silent nil return
  • Confusing LocalJumpError with syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes