Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Functional Patterns in Ruby
What will be the output of this Ruby code?
subtract = ->(a, b) { a - b }
curried_subtract = subtract.curry
subtract_from_ten = curried_subtract.call(10)
puts subtract_from_ten.call(4)
AError
B14
C-6
D6
Step-by-Step Solution
Solution:
  1. Step 1: Define the lambda

    The lambda subtracts the second argument from the first: a - b.
  2. Step 2: Curry the lambda

    Using subtract.curry allows partial application of arguments.
  3. Step 3: Partially apply first argument

    subtract_from_ten = curried_subtract.call(10) fixes a = 10.
  4. Step 4: Call with second argument

    subtract_from_ten.call(4) computes 10 - 4 = 6.
  5. Final Answer:

    6 -> Option D
  6. Quick Check:

    10 minus 4 equals 6 [OK]
Quick Trick: Currying fixes arguments one at a time [OK]
Common Mistakes:
  • Confusing argument order in subtraction
  • Expecting addition instead of subtraction
  • Calling curried function with all arguments at once

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes