MΓ³dulo 25 Β· Blocks, Procs & Lambdas β LecciΓ³n 2 de 6 Β· ~9 min
Procs & Closures
A Proc is a block you can store and pass around
A block isn't an object β you can't put it in a variable or return it. A Proc is: it's a block turned into a first-class object you can store, pass, and call later. This is Ruby's version of a function value / closure, and it's the substrate lambdas are built on.
greet = Proc.new { |name| "Hi #{name}" }
greet.call("Priya") # "Hi Priya"
greet.("Priya") # same thing, .() sugar
greet["Priya"] # same thing, [] sugarIn an interview, say: "A Proc is a callable object that captures the block and the surrounding local variables β a closure. Blocks are the lightweight syntax; procs are blocks promoted to objects."