0
0
Rubyprogramming~20 mins

Lambda creation and behavior in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A4
B12
CError: wrong number of arguments
Dnil
Attempts:
2 left
💡 Hint
Remember that the lambda multiplies the input by 3.
Predict Output
intermediate
2:00remaining
Lambda argument count enforcement
What happens when this Ruby code runs?
Ruby
my_lambda = ->(x, y) { x + y }
puts my_lambda.call(5)
A5 + nil (TypeError)
B5
Cnil
DError: wrong number of arguments (given 1, expected 2)
Attempts:
2 left
💡 Hint
Lambdas check the number of arguments strictly.
Predict Output
advanced
2: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)
A
5
5
B
Error
Error
C
5
Error: wrong number of arguments (given 1, expected 2)
D
nil
Error
Attempts:
2 left
💡 Hint
Procs are lenient with arguments, lambdas are strict.
🧠 Conceptual
advanced
2: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
A
after lambda
from proc
B
after lambda
after proc
C
from lambda
after proc
D
from lambda
from proc
Attempts:
2 left
💡 Hint
Return inside a proc exits the method, return inside a lambda exits only the lambda.
🔧 Debug
expert
2: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)
ANameError because y is not defined inside the lambda's scope at the time of call
BArgumentError because lambda expects 2 arguments but only 1 given
CSyntaxError due to missing parentheses in lambda definition
DNo error, outputs 15
Attempts:
2 left
💡 Hint
Check variable scope inside lambdas.