Bird
0
0

What is wrong with this Ruby code?

medium📝 Debug Q14 of 15
Ruby - Blocks, Procs, and Lambdas
What is wrong with this Ruby code?
my_lambda = lambda { |x, y| x + y }
puts my_lambda.call(5)
AIt will raise an ArgumentError
BIt will print nil
CIt will print 5
DIt will raise a SyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Check lambda argument requirements

    Lambdas check arguments strictly. This lambda expects 2 arguments (x and y).
  2. Step 2: Analyze the call with one argument

    Calling my_lambda.call(5) provides only 1 argument, causing ArgumentError.
  3. Final Answer:

    It will raise an ArgumentError -> Option A
  4. Quick Check:

    Lambda argument count mismatch = ArgumentError [OK]
Quick Trick: Lambdas need exact argument count or error [OK]
Common Mistakes:
  • Assuming missing arguments default to nil
  • Expecting no error on fewer arguments
  • Confusing lambdas with procs (which are lenient)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes