Challenge - 5 Problems
Proc Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a Proc call with arguments
What is the output of this Ruby code when the proc is called?
Ruby
my_proc = Proc.new { |x, y| x * y } result = my_proc.call(3, 4) puts result
Attempts:
2 left
💡 Hint
Remember that Proc.new creates a block that can take arguments and returns the last expression.
✗ Incorrect
The proc multiplies the two arguments 3 and 4, so the output is 12.
❓ Predict Output
intermediate2:00remaining
Proc with missing arguments
What happens when this proc is called with fewer arguments than expected?
Ruby
my_proc = Proc.new { |x, y| x + (y || 0) } puts my_proc.call(5)
Attempts:
2 left
💡 Hint
Procs allow missing arguments and assign nil if not provided.
✗ Incorrect
The proc adds x and y, but y is nil so (y || 0) becomes 0, resulting in 5.
❓ Predict Output
advanced2:00remaining
Difference between Proc and lambda call behavior
What is the output of this code?
Ruby
my_lambda = ->(x, y) { x / y }
my_proc = Proc.new { |x, y| x / y }
puts my_lambda.call(10, 0)
puts my_proc.call(10, 0)Attempts:
2 left
💡 Hint
Both lambda and proc execute the division, which raises an error when dividing by zero.
✗ Incorrect
Both calls raise ZeroDivisionError because dividing by zero is not allowed in Ruby.
❓ Predict Output
advanced2:00remaining
Return behavior inside Proc vs lambda
What is the output of this Ruby code?
Ruby
def test_return my_proc = Proc.new { return 'proc return' } my_lambda = -> { return 'lambda return' } result_proc = my_proc.call result_lambda = my_lambda.call 'method end' end puts test_return
Attempts:
2 left
💡 Hint
A return inside a proc returns from the enclosing method, but a return inside a lambda returns from the lambda only.
✗ Incorrect
The proc's return exits the method immediately with 'proc return', so 'method end' is never reached.
🧠 Conceptual
expert2:00remaining
Which option causes a LocalJumpError?
Which of these code snippets will raise a LocalJumpError when executed?
Attempts:
2 left
💡 Hint
A return inside a proc called outside a method context causes LocalJumpError.
✗ Incorrect
Calling a proc with return outside a method causes LocalJumpError because there's no method to return from.