Procs
Reusable block objects.
Procs is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reusable Block Objects
A block is not an object on its own, so you cannot store it in a variable. A Proc wraps a block into a real object you can save, pass around, and call later.
greeter = Proc.new { puts "Hello!" }
greeter.call
greeter.callCreating Procs
Two common ways to build a Proc: Proc.new { ... } and the shorter proc { ... } Kernel method. Both produce the same kind of object.
double = proc { |x| x * 2 }
p double.call(5)Calling a Proc
You can invoke a Proc several ways: .call, .(), and the square-bracket shorthand [ ]. They are equivalent.
square = proc { |x| x * x }
puts square.call(4)
puts square.(4)
puts square[4]Procs Take Arguments
Procs accept parameters just like blocks, declared between the pipes.
add = proc { |a, b| a + b }
puts add.call(3, 7)Storing Procs in Arrays
Because Procs are objects, you can keep a collection of behaviors and run them in turn.
ops = [proc { |x| x + 1 }, proc { |x| x * 2 }]
result = 5
ops.each { |op| result = op.call(result) }
puts resultPassing a Proc as a Block
Prefix a Proc with & to hand it to a method that expects a block. Ruby unwraps it back into block form.
printer = proc { |n| puts "Item: #{n}" }
[1, 2, 3].each(&printer)Capturing Blocks as Procs
The reverse also works: &block in a method signature captures the given block as a Proc you can store.
def make_handler(&block)
block
end
h = make_handler { |msg| puts "Got: #{msg}" }
h.call("ping")Procs Are Lenient with Arity
A key trait: Procs do not complain about wrong argument counts. Missing arguments become nil and extras are ignored.
p1 = proc { |a, b| puts "a=#{a.inspect} b=#{b.inspect}" }
p1.call(1)
p1.call(1, 2, 3)Procs and return
A return inside a Proc returns from the surrounding method, not just the Proc. This can surprise you, so be careful.
def demo
p = proc { return 10 }
p.call
20 # never reached
end
puts demoProcs Capture Their Surroundings
A Proc remembers the variables in scope where it was defined. This makes it a closure, a topic we explore more later.
factor = 3
times_factor = proc { |n| n * factor }
puts times_factor.call(10)Putting It Together
Procs turn blocks into first-class objects:
- Create with
proc { }orProc.new. - Run with
.call,.(), or[ ]. - Pass to methods with
&. - They are lenient about arity and use the method-level
return.
pipeline = [proc { |s| s.upcase }, proc { |s| s + "!" }]
text = "hello"
pipeline.each { |step| text = step.call(text) }
puts textQuick Check
Test your understanding of Procs.
Recap
You learned Procs:
- A Proc is a block wrapped as an object.
- Build with
proc/Proc.new; run with.call. - Pass to methods using
&. - They tolerate arity mismatches and use a method-level
return.
shout = proc { |word| puts word.upcase }
shout.call("ruby")Frequently asked questions
Is the “Procs” lesson free?
Yes — the full text of “Procs” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Procs”?
Reusable block objects. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Procs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.