Bird
0
0

Fix the error in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Blocks, Procs, and Lambdas
Fix the error in this Ruby code:
my_lambda = ->(x, y) { x + y }
puts my_lambda.call(3)
AChange lambda to proc to allow one argument
BUse my_lambda.call(3) without changes
CRemove one parameter from lambda definition
DAdd a second argument when calling: my_lambda.call(3, 4)
Step-by-Step Solution
Solution:
  1. Step 1: Identify cause of error

    The lambda expects two arguments but is called with one, causing an ArgumentError.
  2. Step 2: Fix by providing correct number of arguments

    Calling with two arguments like my_lambda.call(3, 4) matches the lambda's parameters and fixes the error.
  3. Final Answer:

    Add a second argument when calling: my_lambda.call(3, 4) -> Option D
  4. Quick Check:

    Match lambda args with call args [OK]
Quick Trick: Call lambdas with exact argument count [OK]
Common Mistakes:
  • Calling with fewer arguments
  • Changing lambda to proc without reason
  • Ignoring parameter count mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes