Challenge - 5 Problems
Ruby Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple lambda call
What is the output of this Ruby code?
Ruby
my_lambda = ->(x) { x * 3 }
puts my_lambda.call(4)Attempts:
2 left
💡 Hint
Remember that the lambda multiplies the input by 3.
✗ Incorrect
The lambda takes one argument x and returns x multiplied by 3. Calling it with 4 returns 12.
❓ Predict Output
intermediate2:00remaining
Lambda argument count enforcement
What happens when this Ruby code runs?
Ruby
my_lambda = ->(x, y) { x + y }
puts my_lambda.call(5)Attempts:
2 left
💡 Hint
Lambdas check the number of arguments strictly.
✗ Incorrect
The lambda expects 2 arguments but only 1 is given, so Ruby raises an ArgumentError.
❓ Predict Output
advanced2:00remaining
Difference between lambda and proc in argument handling
What is the output of this Ruby code?
Ruby
my_proc = Proc.new { |x, y| x.to_i + (y || 0) } my_lambda = ->(x, y) { x + y } puts my_proc.call(5) puts my_lambda.call(5)
Attempts:
2 left
💡 Hint
Procs are lenient with arguments, lambdas are strict.
✗ Incorrect
The proc treats missing arguments as nil and uses default 0, so 5 + 0 = 5. The lambda requires exactly 2 arguments and raises an error when only 1 is given.
🧠 Conceptual
advanced2:00remaining
Return behavior inside lambda vs proc
What happens when this Ruby code runs?
Ruby
def test_lambda l = -> { return "from lambda" } l.call "after lambda" end def test_proc p = Proc.new { return "from proc" } p.call "after proc" end puts test_lambda puts test_proc
Attempts:
2 left
💡 Hint
Return inside a proc exits the method, return inside a lambda exits only the lambda.
✗ Incorrect
The lambda's return exits only the lambda block, so the method continues and returns 'after lambda'. The proc's return exits the whole method immediately, returning 'from proc'.
🔧 Debug
expert2:00remaining
Why does this lambda raise an error?
This Ruby code raises an error. Which option explains the cause?
Ruby
my_lambda = ->(x) { x + y }
y = 10
puts my_lambda.call(5)Attempts:
2 left
💡 Hint
Check variable scope inside lambdas.
✗ Incorrect
The variable y is defined after the lambda is created, but the lambda captures the environment at creation. Since y is not in scope inside the lambda when called, a NameError is raised.