Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Blocks, Procs, and Lambdas
Identify the error in this Ruby code:
l = ->(x, y) { x + y }
l.call(5)
ANo error, outputs 5
BSyntaxError due to missing block
CRuntimeError due to return statement
DArgumentError due to wrong number of arguments
Step-by-Step Solution
Solution:
  1. Step 1: Check lambda arity

    The lambda expects exactly 2 arguments (x and y).
  2. Step 2: Analyze call with one argument

    Calling with only 1 argument causes an ArgumentError because lambda checks argument count strictly.
  3. Final Answer:

    ArgumentError due to wrong number of arguments -> Option D
  4. Quick Check:

    Lambda strict arity = C [OK]
Quick Trick: Lambda requires exact args; missing args cause error [OK]
Common Mistakes:
  • Assuming missing args become nil
  • Expecting no error on wrong args
  • Confusing Proc and lambda argument rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes