Proc (Ruby)
A Proc is a block turned into an object you can store in a variable, pass around, and call later with .call. Unlike a block, a Proc is a first-class value.
Ruby
A block isn't an object, but a Proc is — Proc.new { ... } or proc { ... } wraps a block so you can save it, pass it to multiple methods, and invoke it later with proc.call(args) or proc.(args). This makes Procs Ruby's reusable callbacks.
Procs are lenient about arguments: extra arguments are ignored and missing ones become nil, and a return inside a Proc returns from the enclosing method, not just the Proc. These behaviors are the main difference from lambdas, which are strict about arity and return only from themselves.
You can convert a block parameter to a Proc with &block, and convert a Proc (or symbol) back to a block with & at a call site: [1,2,3].map(&:to_s). That &:symbol idiom is one of the most common Ruby shorthands.
Syntax
square = Proc.new { |x| x * x }
square.call(5)Examples
Ruby
double = proc { |x| x * 2 }
puts double.call(4)
puts [1, 2, 3].map(&double).inspectOutput:
8
[2, 4, 6]