Bird
0
0

Identify the error in this Ruby code using currying:

medium📝 Debug Q14 of 15
Ruby - Functional Patterns in Ruby
Identify the error in this Ruby code using currying:
multiply = ->(x, y) { x * y }
curried_multiply = multiply.curry
result = curried_multiply(2)(3)
puts result
Amultiply should be defined with def, not lambda
BNo error, code runs correctly and prints 6
Ccurry method does not exist for lambdas
Dcurried_multiply(2)(3) should be curried_multiply.call(2).call(3)
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax for currying and calling

    In Ruby, lambdas can be curried and called using parentheses like functions.
  2. Step 2: Verify code behavior

    curried_multiply(2) returns a function that multiplies by 2; calling (3) returns 6. The code runs without error.
  3. Final Answer:

    No error, code runs correctly and prints 6 -> Option B
  4. Quick Check:

    Currying with lambdas and calls works as shown [OK]
Quick Trick: Parentheses work for calling curried lambdas [OK]
Common Mistakes:
  • Thinking .call is required for every call
  • Believing curry is not available for lambdas
  • Assuming lambdas must be defined with def

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes