Bird
Raised Fist0
Rubyprogramming~20 mins

Proc creation and call in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Proc Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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
Anil
B12
C7
DError: wrong number of arguments
Attempts:
2 left
💡 Hint
Remember that Proc.new creates a block that can take arguments and returns the last expression.
❓ Predict Output
intermediate
2: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)
A5
BError: wrong number of arguments
Cnil
D0
Attempts:
2 left
💡 Hint
Procs allow missing arguments and assign nil if not provided.
❓ Predict Output
advanced
2: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)
AZeroDivisionError on lambda call, no output on proc call
BZeroDivisionError on lambda call, Infinity on proc call
CZeroDivisionError on lambda call, nil on proc call
DZeroDivisionError on lambda call, ZeroDivisionError on proc call
Attempts:
2 left
💡 Hint
Both lambda and proc execute the division, which raises an error when dividing by zero.
❓ Predict Output
advanced
2: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
Aproc return
Bmethod end
Clambda return
DError: unexpected 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.
🧠 Conceptual
expert
2:00remaining
Which option causes a LocalJumpError?
Which of these code snippets will raise a LocalJumpError when executed?
A
def method
  my_proc = Proc.new { return 5 }
  my_proc.call
  10
end
puts method
B
my_lambda = -> { return 5 }
my_lambda.call
C
my_proc = Proc.new { return 5 }
my_proc.call
D
def method
  my_lambda = -> { return 5 }
  my_lambda.call
  10
end
puts method
Attempts:
2 left
💡 Hint
A return inside a proc called outside a method context causes LocalJumpError.

Practice

(1/5)
1. What does a Proc in Ruby do?
easy
A. Stores a block of code to run later
B. Creates a new variable
C. Defines a class
D. Starts a loop

Solution

  1. Step 1: Understand what a Proc is and match description

    A Proc is a way to save some code to run later, like a recipe you keep for cooking. Only Stores a block of code to run later describes storing code to run later, which matches what a Proc does.
  2. Final Answer:

    Stores a block of code to run later -> Option A
  3. Quick Check:

    Proc = stored code block [OK]
Hint: Proc saves code to run later, like a saved recipe [OK]
Common Mistakes:
  • Thinking Proc creates variables
  • Confusing Proc with class definition
  • Assuming Proc starts loops automatically
2. Which of the following is the correct way to create a Proc in Ruby?
easy
A. my_proc = Proc.new { |x| x * 2 }
B. my_proc = new Proc { |x| x * 2 }
C. my_proc = Proc.create { |x| x * 2 }
D. my_proc = Proc.call { |x| x * 2 }

Solution

  1. Step 1: Recall Proc creation syntax and check options

    In Ruby, you create a Proc using Proc.new or proc with a block. Only my_proc = Proc.new { |x| x * 2 } uses the correct syntax Proc.new { |x| x * 2 }. Others use invalid methods like new Proc, Proc.create, or Proc.call.
  2. Final Answer:

    my_proc = Proc.new { |x| x * 2 } -> Option A
  3. Quick Check:

    Proc.new + block = correct Proc creation [OK]
Hint: Use Proc.new or proc with a block to create a Proc [OK]
Common Mistakes:
  • Using 'new Proc' instead of 'Proc.new'
  • Trying to call Proc.create or Proc.call to create
  • Missing the block braces {}
3. What is the output of this Ruby code?
my_proc = proc { |x| x + 3 }
puts my_proc.call(4)
medium
A. 4
B. Error
C. 3
D. 7

Solution

  1. Step 1: Trace the Proc definition and execution

    The Proc adds 3 to the input number: x + 3. Calling my_proc.call(4) computes 4 + 3 = 7.
  2. Final Answer:

    7 -> Option D
  3. Quick Check:

    4 + 3 = 7 [OK]
Hint: Proc.call runs code with given argument [OK]
Common Mistakes:
  • Confusing Proc.call with Proc.new
  • Forgetting to pass argument to call
  • Expecting output without call
4. Identify the error in this code snippet:
my_proc = Proc.new { |x| x * 2 }
puts my_proc.call
medium
A. Proc.new syntax is incorrect
B. Missing argument when calling the Proc
C. Cannot call Proc with call method
D. Block should not have parameters

Solution

  1. Step 1: Analyze Proc definition and call for errors

    The Proc expects one argument x to multiply by 2. The call is made without any argument, which causes an error because x is missing.
  2. Final Answer:

    Missing argument when calling the Proc -> Option B
  3. Quick Check:

    Proc needs argument when called [OK]
Hint: Always pass required arguments when calling a Proc [OK]
Common Mistakes:
  • Calling Proc without required arguments
  • Thinking Proc.new syntax is wrong
  • Believing call method is invalid
5. Given this code, what will be the output?
def create_proc
  Proc.new { |x| x.nil? ? 'No value' : x * 10 }
end

my_proc = create_proc
puts my_proc.call(nil)
puts my_proc.call(5)
hard
A. nil\n50
B. Error\n50
C. No value\n50
D. No value\n5

Solution

  1. Step 1: Analyze the Proc logic and trace both calls

    The Proc checks if x is nil; if yes, returns 'No value', else returns x * 10. Calling with nil returns 'No value'. Calling with 5 returns 5 * 10 = 50.
  2. Final Answer:

    No value\n50 -> Option C
  3. Quick Check:

    nil -> 'No value', 5 -> 50 [OK]
Hint: Proc can handle nil with conditional inside block [OK]
Common Mistakes:
  • Expecting error when passing nil
  • Forgetting ternary condition inside Proc
  • Assuming multiplication by 10 applies to nil
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard