What Is the Difference Between a Block, a Proc, and a Lambda in Ruby? (2013)
Published on: 2025-06-30 01:34:12
What are blocks, procs, and lambdas?
Coder Talk: Examples of closures in ruby.
Plain old english: Ways of grouping code we want to run.
[ 1 , 2 , 3 ] . each { | x | puts x * 2 } [ 1 , 2 , 3 ] . each do | x | puts x * 2 end p = Proc . new { | x | puts x * 2 } [ 1 , 2 , 3 ] . each ( & p ) proc = Proc . new { puts "Hello World" } proc . call lam = lambda { | x | puts x * 2 } [ 1 , 2 , 3 ] . each ( & lam ) lam = lambda { puts "Hello World" } lam . call
While it looks like these are all very similar, there are subtle differences that I will cover below.
Differences between Blocks and Procs
1. Procs are objects, blocks are not
A proc (notice the lowercase p) is an instance of the Proc class.
p = Proc . new { puts "Hello World" }
This lets us call methods on it and assign it to variables. Procs can also return themselves.
p . call p . class a = p p
In contrast, a block is just part of the syntax of a method call. It doesn’t mean anything on a standalone basis and can only appear in
... Read full article.